// To Hell and Back
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
const int N = 1e5 + 10;
int ps[N], rt[N], S[N], P[9][N], H[N], n, m;
vector<int> adj[N];
int Find(int v) {
return !rt[v] ? v : rt[v] = Find(rt[v]);
}
int LCA(int u, int v) {
if (H[u] < H[v]) swap(u, v);
for (int i = H[u] - H[v], j = 0; i; i /= 5, j++) {
while (i % 5) u = P[j][u], i--;
}
if (u == v) return u;
for (int i = 8; ~i; i--) {
for (int j = 0; j < 4; j++)
if (P[i][u] != P[i][v])
u = P[i][u], v = P[i][v];
}
return P[0][v];
}
void upd(int v, int p) {
P[0][v] = p; H[v] = H[p] + 1;
for (int i = 1; i < 9; i++) {
P[i][v] = P[i - 1][P[i - 1][P[i - 1][P[i - 1][P[i - 1][v]]]]];
}
for (int u : adj[v])
if (u != p) upd(u, v);
}
void Union(int u, int v) {
int pu = Find(u), pv = Find(v);
if (pu == pv) {
ps[u]++, ps[v]++, ps[LCA(u, v)] -= 2;
} else {
if (S[pv] < S[pu]) swap(pu, pv), swap(u, v);
S[pv] += S[pu]; rt[pu] = pv;
adj[u].push_back(v), adj[v].push_back(u);
upd(u, v);
}
}
void DFS(int v, int p = -1) {
S[v] = 1;
for (int u : adj[v])
if (u != p) {
DFS(u, v), ps[v] += ps[u];
if (!ps[u]) printf("%d %d\n", u, v);
}
}
int main() {
fill(S, S + N, 1);
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v; scanf("%d%d", &u, &v);
Union(u, v);
}
memset(S, 0, sizeof S);
for (int i = 1; i <= n; i++)
if (!S[i]) DFS(i);
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:67:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
67 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:69:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
69 | int u, v; scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3052 KB |
Output is correct |
2 |
Incorrect |
2 ms |
3052 KB |
Wrong number of edges |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
3564 KB |
Output is correct |
2 |
Correct |
8 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
225 ms |
3308 KB |
Output is correct |
2 |
Correct |
212 ms |
3436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
450 ms |
3916 KB |
Output is correct |
2 |
Correct |
481 ms |
3948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
746 ms |
5016 KB |
Output is correct |
2 |
Correct |
664 ms |
5484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1202 ms |
8556 KB |
Output is correct |
2 |
Correct |
985 ms |
8556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1974 ms |
9452 KB |
Output is correct |
2 |
Correct |
1643 ms |
9468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2690 ms |
10988 KB |
Output is correct |
2 |
Correct |
2431 ms |
10956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3477 ms |
10932 KB |
Output is correct |
2 |
Correct |
3166 ms |
11036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4149 ms |
10604 KB |
Output is correct |
2 |
Correct |
3796 ms |
11256 KB |
Output is correct |