// 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], from[N], to[N], P[9][N], H[N], n, m;
vector<int> adj[N]; vector<pii> vec;
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 DFS(int v, int p = -1) {
for (int u : adj[v])
if (u != p) {
DFS(u, v), ps[v] += ps[u];
if (ps[u]) vec.push_back({min(u, v), max(u, v)});
ps[u] = 0;
}
}
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;
DFS(pu), ps[pu] = 0;
adj[u].push_back(v), adj[v].push_back(u);
upd(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);
}
DFS(Find(1)); sort(vec.begin(), vec.end());
for (int i = 1; i <= n; i++) {
for (int u : adj[i]) {
if (u < i) continue;
auto it = lower_bound(vec.begin(), vec.end(), pii(i, u));
if (it != vec.end() && *it == pii(i, u)) continue;
printf("%d %d\n", i, u);
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:68:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
68 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:70:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | int u, v; scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3052 KB |
Output is correct |
2 |
Correct |
2 ms |
3052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
3564 KB |
Output is correct |
2 |
Correct |
11 ms |
3564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
228 ms |
3436 KB |
Output is correct |
2 |
Correct |
222 ms |
3564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
421 ms |
4076 KB |
Output is correct |
2 |
Correct |
480 ms |
4204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
752 ms |
5484 KB |
Output is correct |
2 |
Correct |
619 ms |
5868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1153 ms |
9708 KB |
Output is correct |
2 |
Correct |
1002 ms |
11248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1846 ms |
10684 KB |
Output is correct |
2 |
Correct |
1708 ms |
11008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2688 ms |
12012 KB |
Output is correct |
2 |
Correct |
2539 ms |
13468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3318 ms |
12140 KB |
Output is correct |
2 |
Correct |
3234 ms |
13288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4198 ms |
11680 KB |
Output is correct |
2 |
Correct |
3691 ms |
12720 KB |
Output is correct |