QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#146908 | #4194. Killjoys' Conference | xinying | WA | 1ms | 3452kb | C++14 | 1.8kb | 2023-08-22 17:25:56 | 2023-08-22 17:25:57 |
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 ;
}
}
res = (res + 1) % p;
if (ok) {
cout << (res + 1) % p;
} else {
cout << "impossible";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3452kb
input:
4 2 11 1 2 3 4
output:
4
result:
wrong answer 1st lines differ - expected: '3', found: '4'