#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];
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;
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);
}
}
# |
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 |
100 ms |
2944 KB |
Output is correct |
2 |
Correct |
101 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
3200 KB |
Output is correct |
2 |
Correct |
199 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
3784 KB |
Output is correct |
2 |
Correct |
263 ms |
3948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
406 ms |
5896 KB |
Output is correct |
2 |
Correct |
353 ms |
5768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
634 ms |
6248 KB |
Output is correct |
2 |
Correct |
623 ms |
6392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
835 ms |
7176 KB |
Output is correct |
2 |
Correct |
792 ms |
7148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1180 ms |
7036 KB |
Output is correct |
2 |
Correct |
992 ms |
7060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1217 ms |
6820 KB |
Output is correct |
2 |
Correct |
1245 ms |
7180 KB |
Output is correct |