#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
const int maxn = 100010;
int N, M;
const int mod = 1000000007;
int nums[maxn];
vector<int> adj[maxn];
int par[maxn];
int findset(int a) {
if (par[a] == a) return a;
return par[a] = findset(par[a]);
}
void unionset(int a, int b) {
a = findset(a);
b = findset(b);
if (a == b) return;
if (rand() % 2) {
par[a] = b;
}
else par[b] = a;
}
bool vis[maxn];
// int val;
void dfs(int u, int p) {
vis[u] = true;
for (int val : adj[u]) {
if (val != p) {
dfs(val, u);
nums[u] = (nums[val]+nums[u])%mod;
}
}
if (nums[u] == 0 && p != -1) {
cout << u << " " << p << endl;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for (int i = 1; i <= N; i++) par[i] = i;
srand(23); //want deterministic for now
int x, y;
int cur;
for (int i = 0; i < M; i++) {
cin >> x >> y;
if (findset(x) != findset(y)) {
adj[x].push_back(y);
adj[y].push_back(x);
unionset(x, y);
}
else {
cur = rand()%mod;
nums[x] += cur + 0LL;
nums[y] += mod-cur + 0LL;
nums[x]%=mod;
nums[y]%=mod;
}
}
for (int i = 1; i <= N; i++) {
if (!vis[i]) dfs(i, -1);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2752 KB |
Output is correct |
2 |
Correct |
4 ms |
2660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2816 KB |
Output is correct |
2 |
Correct |
7 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
2884 KB |
Output is correct |
2 |
Correct |
101 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
3116 KB |
Output is correct |
2 |
Correct |
200 ms |
3112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
307 ms |
3676 KB |
Output is correct |
2 |
Correct |
277 ms |
3832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
408 ms |
5480 KB |
Output is correct |
2 |
Correct |
382 ms |
5496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
608 ms |
5908 KB |
Output is correct |
2 |
Correct |
628 ms |
5880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
844 ms |
6640 KB |
Output is correct |
2 |
Correct |
775 ms |
6664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1061 ms |
6644 KB |
Output is correct |
2 |
Correct |
1027 ms |
6660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1249 ms |
6448 KB |
Output is correct |
2 |
Correct |
1295 ms |
6708 KB |
Output is correct |