#include <utility>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
typedef pair<int,int> ii;
int n,m;
int d[100005], pa[100005], sz[100005];
int bad[100005];
vector<int> G[100005];
struct ufds{
int p[100005];
ufds(){
for (int i = 0; i <= 100000; i++) p[i] = i;
}
int find(int x){
return p[x] == x ? x : p[x] = find(p[x]);
}
void un(int x, int y){
p[x] = y;
}
} u1 = ufds(), u2 = ufds();
void dfs(int u, int p){
pa[u] = p;
for (auto v : G[u]){
if (v == p) continue;
else{
d[v] = d[u]+1;
pa[v] = u;
dfs(v,u);
}
}
}
int main(){
scanf("%d%d",&n,&m);
for (int i = 0; i < m; i++){
int u,v;
scanf("%d%d",&u,&v);
int pu = u1.find(u), pv = u1.find(v);
//printf("%d -> %d, %d -> %d\n",u,pu,v,pv);
if (pu == pv){
//printf("update path %d %d\n",u,v);
///make all edges along this path bad
///all edges along path are only changed once, O(N)
u = u2.find(u), v = u2.find(v);
while (u != v){
if (d[u] < d[v]) swap(u,v);
bad[u] = true;
//printf("bad %d %d\n",u,pa[u]);
u = pa[u];
u = u2.find(u);
}
}
else{
//printf("join %d %d\n",u,v);
///join the two components
if (sz[pu] < sz[pv]){
swap(pu,pv);
swap(u,v);
}
///join small v to big u
u1.un(pv,pu);
sz[u] += sz[v];
///add edge
G[u].push_back(v);
G[v].push_back(u);
///restart dfs
d[v] = d[u]+1;
dfs(v,u);
}
}
for (int i = 1 ; i<= n; i++){
if (!bad[i] && pa[i] != 0){
printf("%d %d\n",i,pa[i]);
}
}
}
Compilation message
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:39:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&u,&v);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
3456 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
3712 KB |
Wrong number of edges |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
405 ms |
9020 KB |
Output is correct |
2 |
Incorrect |
271 ms |
8952 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1275 ms |
13536 KB |
Output is correct |
2 |
Incorrect |
783 ms |
14072 KB |
Wrong number of edges |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4576 ms |
20928 KB |
Memory limit exceeded (if you are sure your verdict is not MLE, please contact us) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5053 ms |
7296 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5043 ms |
7780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5062 ms |
8588 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5061 ms |
8400 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5053 ms |
8324 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |