#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<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 |
3 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 |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
2816 KB |
Output is correct |
2 |
Correct |
97 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
3192 KB |
Output is correct |
2 |
Correct |
191 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
3776 KB |
Output is correct |
2 |
Correct |
260 ms |
4136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
394 ms |
5768 KB |
Output is correct |
2 |
Correct |
364 ms |
5884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
699 ms |
6232 KB |
Output is correct |
2 |
Correct |
631 ms |
6264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
857 ms |
7036 KB |
Output is correct |
2 |
Correct |
794 ms |
7040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1322 ms |
7020 KB |
Output is correct |
2 |
Correct |
1016 ms |
7048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1233 ms |
6812 KB |
Output is correct |
2 |
Correct |
1274 ms |
7148 KB |
Output is correct |