QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#54612#4194. Killjoys' ConferenceT3alaadl3k2olyehymn3k#WA 949ms80444kbC++2.7kb2022-10-09 20:44:282022-10-09 20:44:30

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-09 20:44:30]
  • 评测
  • 测评结果:WA
  • 用时:949ms
  • 内存:80444kb
  • [2022-10-09 20:44:28]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define gtr(T) vector<T>,greater<T>
#define int long long
using namespace std;
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

void err(istream_iterator<string> it) { cerr << endl; }

template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}

using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;


const int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};


const int N = 1e6 + 5;
const int M = 1e3 + 5;
const int MOD = 100;

int n, m;

vector<int> adj[N];
int vis[N], parent[N];
bool isCycle = false;
int p;

void addEdge(int u, int v) {
    adj[u].emplace_back(v);
    adj[v].emplace_back(u);
}

bool dfs(int v, int par) { // passing vertex and its parent vertex
    vis[v] = true;
    for (int u: adj[v]) {
        if (u == par) continue; // skipping edge to parent vertex
        if (vis[u]) {
            isCycle = 1;
            return true;
        }
        parent[u] = v;
        if (dfs(u, parent[u])) {
            return true;
        }
    }
    return false;
}

int add(int a, int b, int p) {
    return (a + b) % p;
}

int mul(int a, int b, int p) {
    return (1LL * a * b) % p;
}

ll fp(ll base, ll power, ll M) {
    if (power == 0)return 1;
    if (power == 1)
        return base;
    ll val = fp(base, power / 2, M);
    return (power % 2 == 0) ? (val * val) % M : (((val * val) % M) * base) % M;
}

void dfs(int u) {
    if (vis[u])return;
    vis[u] = 1;
    for (auto i: adj[u])
        dfs(i);
}

signed main() {
    memset(parent, -1, sizeof parent);
    memset(vis, 0, sizeof vis);
    cin >> n >> m >> p;
    int u, v;
    for (int i = 0; i < m; ++i) {
        cin >> u >> v;
        addEdge(u, v);
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
        if (!vis[i]) {
            cnt++;
            dfs(i);
        };
    memset(vis, 0, sizeof vis);
    for (int i = 1; i <= n; ++i) {
        if (!vis[i] and dfs(i, parent[i])) {
            break;
        }
    }
    if (isCycle) {
        cout << "impossible\n";
    } else {
        int ans = fp(2, cnt - 1, p);
        ans = add(ans, 1, p);
        cout << ans << endl;
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 42612kb

input:

4 2 11
1 2
3 4

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 11ms
memory: 42644kb

input:

5 2 3
1 2
3 4

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 2ms
memory: 42628kb

input:

3 3 11
1 2
2 3
3 1

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 8ms
memory: 42792kb

input:

100 0 13

output:

9

result:

ok single line: '9'

Test #5:

score: 0
Accepted
time: 13ms
memory: 42744kb

input:

1000000 0 999983

output:

131073

result:

ok single line: '131073'

Test #6:

score: 0
Accepted
time: 12ms
memory: 42724kb

input:

5 0 17

output:

0

result:

ok single line: '0'

Test #7:

score: 0
Accepted
time: 8ms
memory: 42672kb

input:

6 0 11

output:

0

result:

ok single line: '0'

Test #8:

score: 0
Accepted
time: 949ms
memory: 80444kb

input:

910292 929828 296851369
138316 521196
344174 594024
83412 434709
773248 836155
238718 422888
306559 904462
267860 898937
413132 488904
377100 725377
280966 647857
194879 226916
46886 181828
211614 264486
597301 659354
211498 340231
131349 463058
218728 850468
112991 832910
311079 403177
94372 457800...

output:

impossible

result:

ok single line: 'impossible'

Test #9:

score: 0
Accepted
time: 44ms
memory: 44772kb

input:

474415 39086 193520539
226365 326319
173587 188446
196237 384686
12130 438747
142687 240963
42823 198839
33523 441119
37690 269573
158873 264117
265666 307648
371107 470830
111014 416679
205426 259164
143776 337091
266405 399531
7332 293732
87282 131377
144755 269135
73861 213137
413795 429757
12293...

output:

24850764

result:

ok single line: '24850764'

Test #10:

score: 0
Accepted
time: 86ms
memory: 46644kb

input:

886771 69103 843021961
155346 347704
395735 428613
145842 769876
730641 854763
368391 433651
570964 775530
482006 716549
411782 547430
22514 663720
7673 93526
234498 340129
126566 579992
627671 739879
683991 787364
64669 613364
361636 539521
82596 581375
151505 311499
214280 232615
429695 706186
102...

output:

195055087

result:

ok single line: '195055087'

Test #11:

score: 0
Accepted
time: 53ms
memory: 45724kb

input:

261871 56947 37434461
181348 220749
60472 229420
22876 33088
78741 90153
52949 210249
82780 241798
19262 56861
83377 141486
199523 211244
144337 168415
124052 145687
21941 74986
138530 230058
108522 125335
24900 44205
40874 58680
27342 173854
96226 169565
42306 77188
88277 143247
35849 229572
28335 ...

output:

29316196

result:

ok single line: '29316196'

Test #12:

score: 0
Accepted
time: 110ms
memory: 48104kb

input:

730267 96959 263495809
161273 375572
255354 562714
173645 232299
282893 328384
211088 298409
8990 696289
431898 554175
678197 701660
10563 605490
263785 696678
133569 330504
121 635357
549515 559844
571064 698976
523289 710717
364053 423410
20210 423915
306989 357164
241145 404451
35863 426544
35402...

output:

183324026

result:

ok single line: '183324026'

Test #13:

score: 0
Accepted
time: 77ms
memory: 46496kb

input:

317597 79392 370638017
85444 281488
86255 250036
59640 187426
91541 197016
115564 151195
127964 281637
163111 256617
78576 275740
133367 292829
63773 108615
5562 102599
53140 145288
69160 93021
8925 186049
70808 268881
20656 254231
71089 203880
184543 237436
14565 67134
220007 234899
76369 270335
87...

output:

48131608

result:

ok single line: '48131608'

Test #14:

score: -100
Wrong Answer
time: 8ms
memory: 42648kb

input:

10 10 999983
1 2
2 3
3 4
1 4
5 6
6 7
7 8
8 9
9 10
5 10

output:

impossible

result:

wrong answer 1st lines differ - expected: '3', found: 'impossible'