# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
54979 |
2018-07-05T15:36:39 Z |
kdh9949 |
Pipes (CEOI15_pipes) |
C++17 |
|
1515 ms |
8056 KB |
#include <bits/stdc++.h>
using namespace std;
const int N = 100001, H = 17;
int n, m, p[N], P[N], c[N], d[N], s[N];
vector<int> e[N];
int f(int x){ return p[x] = (x == p[x] ? x : f(p[x])); }
int F(int x){ return P[x] = (x == P[x] ? x : F(P[x])); }
void g(int x, int y, int z){
for(int i : e[x]) if(i != y) g(i, x, z + 1);
if(s[x] != y && x != f(x)) P[s[x]] = (P[x] == x ? s[x] : x);
d[x] = z; s[x] = y;
}
int main(){
scanf("%d%d", &n, &m);
iota(p, p + n + 1, 0);
iota(P, P + n + 1, 0);
iota(s, s + 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(y, x, d[x] + 1); P[y] = y;
e[x].push_back(y); e[y].push_back(x);
c[f(x)] += c[f(y)]; p[f(y)] = f(x);
}
else{
x = F(x); y = F(y);
while(x != y){
if(d[x] > d[y]){ P[x] = F(P[s[x]]); x = F(x); }
else{ P[y] = F(P[s[y]]); y = F(y); }
}
}
}
for(int i = 1; i <= n; i++) if(P[i] == i && s[i] != i) printf("%d %d\n", i, s[i]);
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:19: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:25: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 |
1 |
Correct |
3 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2944 KB |
Output is correct |
2 |
Correct |
8 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
2816 KB |
Output is correct |
2 |
Correct |
116 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
3200 KB |
Output is correct |
2 |
Correct |
248 ms |
3200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
348 ms |
3832 KB |
Output is correct |
2 |
Correct |
334 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
464 ms |
6392 KB |
Output is correct |
2 |
Correct |
437 ms |
6292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
751 ms |
7020 KB |
Output is correct |
2 |
Correct |
724 ms |
6764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
981 ms |
7876 KB |
Output is correct |
2 |
Correct |
941 ms |
7932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1347 ms |
7796 KB |
Output is correct |
2 |
Correct |
1215 ms |
8056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1515 ms |
7772 KB |
Output is correct |
2 |
Correct |
1433 ms |
7928 KB |
Output is correct |