#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define maxn 100010
int N, M;
const int mod = 1000000007;
ll nums[maxn];
vector<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];
}
}
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;
while (M--) {
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] -= cur + 0LL;
}
}
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 |
4 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2944 KB |
Output is correct |
2 |
Correct |
7 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
2904 KB |
Output is correct |
2 |
Correct |
99 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
3164 KB |
Output is correct |
2 |
Correct |
194 ms |
3156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
3780 KB |
Output is correct |
2 |
Correct |
264 ms |
4124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
391 ms |
5776 KB |
Output is correct |
2 |
Correct |
349 ms |
5772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
595 ms |
6228 KB |
Output is correct |
2 |
Correct |
622 ms |
6256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
844 ms |
7044 KB |
Output is correct |
2 |
Correct |
790 ms |
7068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1147 ms |
7044 KB |
Output is correct |
2 |
Correct |
983 ms |
7056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1273 ms |
6820 KB |
Output is correct |
2 |
Correct |
1316 ms |
7136 KB |
Output is correct |