#include <vector>
#include <cstdio>
using namespace std;
#define MAXN 100005
bool isBridge[MAXN];
int p[MAXN];
int sz[MAXN];
int depth[MAXN];
vector<int> adjl[MAXN];
int p2[MAXN];
int rt[MAXN];
void dfs(int node, int parent){
for (int x : adjl[node]){
if (x==parent) continue;
depth[x] = depth[node]+1;
rt[x] = rt[node];
dfs(x,node);
if(p2[x]!=node){
isBridge[x] = isBridge[node];
p2[x] = node;
}
p[x] = node;
}
}
int func(int a, int b){
// printf("a: %d, depth: %d\n",a,depth[a]);
// printf("b: %d, depth: %d\n",b,depth[b]);
if (a==b) return a;
if (depth[b]<depth[a]) swap(a,b);
isBridge[b] = false;
int t = func(a,p[b]);
p[b] = t;
}
int n,e;
int main(){
//printf("%d\n",sizeof(isBridge)+sizeof(p)+sizeof(sz)+sizeof(depth)+sizeof(adjl)+sizeof(p2)+sizeof(rt));
scanf("%d%d",&n,&e);
for (int x = 0; x<=n; x++){
isBridge[x] = false;
p[x] = x;
sz[x] = 1;
depth[x] = 0;
rt[x] = x;
}
for (int x = 0; x<e; x++){
int a,b;
scanf("%d%d",&a,&b);
if (rt[a]==rt[b]){
func(a,b);
}
else{
if (sz[rt[b]]>sz[rt[a]]) swap(a,b);
sz[rt[a]] += sz[rt[b]];
adjl[b].push_back(a);
adjl[a].push_back(b);
depth[b] = depth[a]+1;
p2[b] = a;
p[b] = a;
// printf("p[%d] set to %d\n",b,a);
rt[b] = rt[a];
dfs(b,a);
isBridge[b] = true;
}
/*for (int x = 1; x<=n; x++){
printf("p[%d] = %d, depth %d, isbridge %d, p: %d\n",x,p2[x],depth[x],isBridge[x], p[x]);
}*/
}
for (int x = 1; x<=n; x++){
if (isBridge[x]){
printf("%d %d\n",x,p2[x]);
}
}
}
Compilation message
pipes.cpp: In function 'int func(int, int)':
pipes.cpp:37:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
pipes.cpp: In function 'int main()':
pipes.cpp:43:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&e);
~~~~~^~~~~~~~~~~~~~
pipes.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&a,&b);
~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
2944 KB |
Output is correct |
2 |
Correct |
10 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
2816 KB |
Output is correct |
2 |
Correct |
155 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
3200 KB |
Output is correct |
2 |
Correct |
323 ms |
3200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
446 ms |
3840 KB |
Output is correct |
2 |
Correct |
385 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
584 ms |
7380 KB |
Output is correct |
2 |
Correct |
511 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
944 ms |
6952 KB |
Output is correct |
2 |
Correct |
924 ms |
6920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1226 ms |
8768 KB |
Output is correct |
2 |
Correct |
1177 ms |
7800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1561 ms |
7800 KB |
Output is correct |
2 |
Correct |
1434 ms |
8008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1887 ms |
7672 KB |
Output is correct |
2 |
Correct |
1861 ms |
7928 KB |
Output is correct |