#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 2000000000
const int N = 100010;
int n, m , cnt = 0 , dsu[N] , dsu2[N] , comp[N] , u , v , a ,b , depth[N] , qu[N], st , en, node;
bitset < N > bridge;
vector< pair<int,int> > g[N];
pair<int,int> parent[N];
inline int find(int u){ return (u == dsu[u] ? u : dsu[u] = find(dsu[u]));}
inline int find2(int u){ return (u == dsu2[u] ? u : dsu2[u] = find2(dsu2[u]));}
inline int get(int node1,int node2){
if(node1 == node2) return find2(node1);
if(depth[node1] > depth[node2]){
bridge[parent[node1].second] = false;
return dsu2[node1] = dsu2[node2] = get(find2(parent[node1].first),node2);
}
bridge[parent[node2].second] = false;
return dsu2[node1] = dsu2[node2] = get(node1,find2(parent[node2].first));
}
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) dsu[i] = dsu2[i] = i , comp[i] = 1;
for(int i=0;i<m;i++){
scanf("%d%d",&u,&v);
a = find(u);
b = find(v);
if(a == b){
get(find2(u),find2(v));
}
else{
g[u].push_back(make_pair(v,cnt));
g[v].push_back(make_pair(u,cnt));
bridge[cnt] = true;
if(comp[a] > comp[b]){swap(a,b);swap(u,v);}
st = 0 , en = 1;
qu[0] = u;
parent[u] = make_pair(v,cnt);
depth[u] = depth[v] + 1;
dsu2[u] = u;
while(st < en){
node = qu[st++];
for(int i=0;i<g[node].size();i++){
if(g[node][i].first != parent[node].first){
qu[en++] = g[node][i].first;
parent[g[node][i].first] = make_pair(node,g[node][i].second);
depth[g[node][i].first] = depth[node] + 1;
if(bridge[g[node][i].second]) dsu2[g[node][i].first] = g[node][i].first; else dsu2[g[node][i].first] = find2(dsu2[node]);
}
}
}
comp[b] += comp[a];
comp[a] = 0;
dsu[a] = b;
cnt++;
}
}
for(int i=1;i<=n;i++){
for(int j=0;j<g[i].size();j++){
if(i > g[i][j].first || !bridge[g[i][j].second]) continue;
printf("%d %d\n",i,g[i][j].first);
}
}
return 0;
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:46:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<g[node].size();i++){
~^~~~~~~~~~~~~~~
pipes.cpp:62:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<g[i].size();j++){
~^~~~~~~~~~~~
pipes.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
pipes.cpp:28:8: 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 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
2944 KB |
Output is correct |
2 |
Correct |
8 ms |
2956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
113 ms |
2944 KB |
Output is correct |
2 |
Correct |
108 ms |
2952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
3200 KB |
Output is correct |
2 |
Correct |
228 ms |
3304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
4096 KB |
Output is correct |
2 |
Correct |
272 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
423 ms |
6908 KB |
Output is correct |
2 |
Correct |
379 ms |
6904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
679 ms |
7416 KB |
Output is correct |
2 |
Correct |
671 ms |
7352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
886 ms |
8632 KB |
Output is correct |
2 |
Correct |
888 ms |
8852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1116 ms |
8536 KB |
Output is correct |
2 |
Correct |
1067 ms |
8824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1361 ms |
8312 KB |
Output is correct |
2 |
Correct |
1334 ms |
8440 KB |
Output is correct |