This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 100001, H = 17;
int n, m, p[N], c[N], v[N], w[N], d[N], s[N][17], q[N];
vector<int> e[N];
int f(int x){ return p[x] = (x == p[x] ? x : f(p[x])); }
int g(int x, int y, int z){
if(z) c[x] = 1;
for(int i : e[x]) if(i != y) v[x] += g(i, x, z);
v[x] += w[x]; w[x] = 0;
if(y && z && !v[x]) printf("%d %d\n", x, y);
return v[x];
}
void h(int x, int y, int z){
s[x][0] = y;
for(int i = 1; i < H; i++) s[x][i] = s[s[x][i - 1]][i - 1];
for(int i : e[x]) if(i != y) h(i, x, z + 1);
}
int a(int x, int y){
if(d[x] < d[y]) swap(x, y);
for(int i = H - 1; i >= 0; i--) if(d[x] - (1 << i) >= d[y]) x = s[x][i];
if(x == y) return x;
for(int i = H - 1; i >= 0; i--) if(s[x][i] != s[y][i]){
x = s[x][i]; y = s[y][i];
}
return s[x][0];
}
int main(){
scanf("%d%d", &n, &m);
iota(p, p + n + 1, 0);
fill(c + 1, c + n + 1, 1);
for(int x, y; m--; ){
scanf("%d%d", &x, &y);
if(f(x) != f(y)){
if(c[f(x)] < c[f(y)]) swap(x, y);
g(f(y), 0, 0);
e[x].push_back(y); e[y].push_back(x);
h(y, x, d[x] + 1);
c[f(x)] += c[f(y)]; p[f(y)] = f(x);
}
else{ w[x]++; w[y]++; w[a(x, y)] -= 2; }
}
fill(c + 1, c + n + 1, 0);
for(int i = 1; i <= n; i++) if(!c[i]) g(i, 0, 1);
}
Compilation message (stderr)
pipes.cpp: In function 'int main()':
pipes.cpp:36:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
pipes.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |