#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 |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 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 |
102 ms |
2888 KB |
Output is correct |
2 |
Correct |
102 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
174 ms |
3116 KB |
Output is correct |
2 |
Correct |
197 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
297 ms |
3668 KB |
Output is correct |
2 |
Correct |
270 ms |
3904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
418 ms |
5488 KB |
Output is correct |
2 |
Correct |
371 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
620 ms |
5912 KB |
Output is correct |
2 |
Correct |
650 ms |
5912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
860 ms |
6788 KB |
Output is correct |
2 |
Correct |
769 ms |
6672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1024 ms |
6636 KB |
Output is correct |
2 |
Correct |
981 ms |
6768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1271 ms |
6444 KB |
Output is correct |
2 |
Correct |
1284 ms |
6736 KB |
Output is correct |