#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);
vector<pii> ans;
int par[maxn];
ll csum[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];
void dfs(int u, int p) {
csum[u] = nums[u];
vis[u] = true;
for (int val : adj[u]) {
if (val != p) {
dfs(val, u);
csum[u] += csum[val];
}
}
if (csum[u] == 0 && p != -1) {
ans.push_back(pii(u, p));
}
}
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;
while (M--) {
cin >> x >> y;
if (findset(x) != findset(y)) {
adj[x].push_back(y);
adj[y].push_back(x);
unionset(x, y);
}
else {
int cur = rand()%mod;
nums[x] += cur + 0LL;
nums[y] -= cur + 0LL;
}
}
for (int i = 1; i <= N; i++) {
if (!vis[i]) dfs(i, -1);
}
for (pii pa : ans) {
cout << pa.first << " " << pa.second << endl;
}
}
# |
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 |
102 ms |
2908 KB |
Output is correct |
2 |
Correct |
100 ms |
2896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
3168 KB |
Output is correct |
2 |
Correct |
195 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
344 ms |
3908 KB |
Output is correct |
2 |
Correct |
265 ms |
4272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
403 ms |
6392 KB |
Output is correct |
2 |
Correct |
345 ms |
6284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
633 ms |
6868 KB |
Output is correct |
2 |
Correct |
626 ms |
6816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
827 ms |
7808 KB |
Output is correct |
2 |
Correct |
872 ms |
7832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1075 ms |
7808 KB |
Output is correct |
2 |
Correct |
996 ms |
7820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1254 ms |
7696 KB |
Output is correct |
2 |
Correct |
1247 ms |
7876 KB |
Output is correct |