QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#146905 | #4194. Killjoys' Conference | xinying | WA | 412ms | 106012kb | C++14 | 1.7kb | 2023-08-22 17:24:30 | 2023-08-22 17:24:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6 + 10;
using ll = long long;
int fa[N];
ll poww(ll a, ll b, ll p){
ll res = 1 % p;
while(b){
if(b & 1) res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
int fx = fa[x], fy = fa[y];
fa[fx] = fy;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, p;
cin >> n >> m >> p;
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
std::vector<std::vector<int>> g(n + 1);
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
bool ok = true;
std::vector<int> col(n + 1, -1);
function<void(int, int)> dfs = [&](int x, int f) {
for (auto y : g[x]) {
if (y == f) continue;
if (col[y] != -1 && (col[x] == col[y])) {
ok = false;
}
if (col[y] == -1) {
col[y] = col[x] ^ 1;
dfs(y, x);
merge(x, y);
}
}
};
int res = 1;
for (int i = 1; i <= n; i++) {
if (col[i] == -1) {
col[i] = 0;
dfs(i, -1);
}
}
std::vector<int> vis(n + 1);
int flag = 0;
for (int i = 1; i <= n; i++) {
if (!vis[find(i)]) {
if (flag)
res = res * 2 % p;
vis[find(i)] = 1;
flag =1 ;
}
}
if (ok) {
cout << (res + 1) % p;
} else {
cout << "impossible";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
input:
4 2 11 1 2 3 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 1ms
memory: 3536kb
input:
5 2 3 1 2 3 4
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
3 3 11 1 2 2 3 3 1
output:
impossible
result:
ok single line: 'impossible'
Test #4:
score: 0
Accepted
time: 1ms
memory: 3656kb
input:
100 0 13
output:
9
result:
ok single line: '9'
Test #5:
score: 0
Accepted
time: 13ms
memory: 50164kb
input:
1000000 0 999983
output:
131073
result:
ok single line: '131073'
Test #6:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
5 0 17
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 1ms
memory: 3636kb
input:
6 0 11
output:
0
result:
ok single line: '0'
Test #8:
score: 0
Accepted
time: 412ms
memory: 106012kb
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: -100
Wrong Answer
time: 15ms
memory: 28988kb
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:
79618820
result:
wrong answer 1st lines differ - expected: '24850764', found: '79618820'