#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb emplace_back
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ii,ii> i4;
const int MOD=1000000007;
const int INF=1012345678;
const ll LLINF=1012345678012345678LL;
const double PI=3.1415926536;
const double EPS=1e-14;
const int MAXN=100005;
int n,m;
int par[MAXN],par2[MAXN]; // par, depth
int root(int x){return par[x]=(par[x]==x?x:root(par[x]));}
int root2(int x){return par2[x]=(par2[x]==x?x:root2(par2[x]));}
vector<int> adj[MAXN]; // only stores at most 2*tree
int low[MAXN];
int ctr=0;
void dfs(int x){
par2[x]=low[x]=ctr++;
bool p=0;
for(int i:adj[x]){
if(i==par[x]&&!p){
p=1;
continue;
}
if(par2[i]==-1){
par[i]=x;
dfs(i);
low[x]=min(low[x],low[i]);
if(low[i]>par2[x])printf("%d %d\n",i+1,x+1);
}else low[x]=min(low[x],par2[i]);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)par[i]=par2[i]=i;
int a,b;
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);a--;b--;
//if a,b are in different trees, merge the two trees
int ra=root(a),rb=root(b);
if(ra!=rb){
par[ra]=rb;
adj[a].pb(b);
adj[b].pb(a);
continue;
}
//otherwise, take note that later we have to get rid of the entire cycle
int r2a=root2(a),r2b=root2(b);
if(r2a!=r2b){
par2[r2a]=par2[r2b];
adj[a].pb(b);
adj[b].pb(a);
}
}
memset(par,-1,sizeof(par));
memset(par2,-1,sizeof(par2));
memset(low,-1,sizeof(low));
for(int i=0;i<n;i++)if(par2[i]==-1)dfs(i);
}
Compilation message
pipes.cpp: In function 'int main()':
pipes.cpp:46:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
pipes.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d",&a,&b);a--;b--;
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3788 KB |
Output is correct |
2 |
Correct |
2 ms |
3788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4172 KB |
Output is correct |
2 |
Correct |
6 ms |
3916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
4096 KB |
Output is correct |
2 |
Correct |
114 ms |
3908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
4768 KB |
Output is correct |
2 |
Correct |
226 ms |
4248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
316 ms |
6076 KB |
Output is correct |
2 |
Correct |
293 ms |
5624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
420 ms |
10612 KB |
Output is correct |
2 |
Correct |
407 ms |
7276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
707 ms |
11604 KB |
Output is correct |
2 |
Correct |
620 ms |
8756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
988 ms |
13588 KB |
Output is correct |
2 |
Correct |
858 ms |
8580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1096 ms |
13564 KB |
Output is correct |
2 |
Correct |
1115 ms |
10824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1398 ms |
13056 KB |
Output is correct |
2 |
Correct |
1334 ms |
14200 KB |
Output is correct |