QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#146887 | #4194. Killjoys' Conference | xinying | WA | 548ms | 117312kb | C++14 | 1.8kb | 2023-08-22 17:18:21 | 2023-08-22 17:18:22 |
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], sz[N];
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;
sz[fy] += sz[fx];
}
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;
sz[i] = 1;
}
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);
for (int i = 1; i <= n; i++) {
if (!vis[find(i)]) {
res = res * 2 % p;
vis[find(i)] = 1;
}
}
const int MOD = p;
auto power = [&](ll a, ll b) {
ll ans = 1;
for (; b; b /= 2, a = a * a % MOD) {
if (b % 2) {
ans = ans * a % MOD;
}
}
return ans % MOD;
};
if (ok) {
cout << (res * power(2, p - 2) % p + 1) % p;
} else {
cout << "impossible";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5508kb
input:
4 2 11 1 2 3 4
output:
3
result:
ok single line: '3'
Test #2:
score: 0
Accepted
time: 2ms
memory: 5588kb
input:
5 2 3 1 2 3 4
output:
2
result:
ok single line: '2'
Test #3:
score: 0
Accepted
time: 2ms
memory: 5544kb
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: 5520kb
input:
100 0 13
output:
9
result:
ok single line: '9'
Test #5:
score: 0
Accepted
time: 13ms
memory: 57760kb
input:
1000000 0 999983
output:
131073
result:
ok single line: '131073'
Test #6:
score: 0
Accepted
time: 2ms
memory: 5516kb
input:
5 0 17
output:
0
result:
ok single line: '0'
Test #7:
score: 0
Accepted
time: 2ms
memory: 5572kb
input:
6 0 11
output:
0
result:
ok single line: '0'
Test #8:
score: 0
Accepted
time: 548ms
memory: 117312kb
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: 28ms
memory: 35328kb
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'