【模板】割边
介绍
- 何为割边
与割点相链接的边(没啦)
- 算法原理
计算出割点,在if里面的时候记录边
Code
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+12;
int u,v,n,m,dfn[N],low[N];
int id,cnt,f[N];
struct node{
int x,y;
bool operator < ( const node &other )
{
if( x==other.x ) return y<other.y;
return x<other.x;
}
}ans[N];
vector<int>edge[N];
void tarjan(int x)
{
dfn[x]=low[x]=++id;
for( int y:edge[x] )
{
if( !dfn[y] )
{
f[y]=x;
tarjan(y);
low[x]=min(low[x],low[y]);
if( low[y]>dfn[x] )
{
ans[++cnt]={ min(x,y),max(x,y) };
}
}
else if( y!=f[x] ) low[x]=min(low[x],dfn[y]);
}
}
int main(){
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
for(int i=1;i<=n;i++)
{
if( !dfn[i] )
{
tarjan(i);
}
}
sort(ans+1,ans+1+cnt);
for(int i=1;i<=cnt;i++)
{
printf("%d %d\n",ans[i].x,ans[i].y);
}
}