This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// 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 (stderr)
pipes.cpp: In function 'int main()':
pipes.cpp:68:14: 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:28: 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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |