# 割点与桥

#include"iostream"
#include"algorithm"
#include"vector"
using namespace std;
const int v=1000;
int edge[v][v];
int bridge[v][v],cut[v];
int low[v],dfn[v],vis[v];
void cut_bridge(int cur,int father,int dep,int n)
{
vis[cur]=1;
dfn[cur]=low[cur]=dep;
int children=0;
for(int i=0;i<n;i++)
if(edge[cur][i])
{
if(i!=father&&vis[i]==1)
{
if(dfn[i]<low[cur])
low[cur]=dfn[i];
}
if(vis[i]==0)
{
cut_bridge(i,cur,dep+1,n);
children++;
if(low[i]<low[cur]) low[cur]=low[i];
if((father==-1&&children>1)||(father!=-1&&low[i]>=dfn[cur]))
cut[cur]=true;
if(low[i]>dfn[cur])
{
bridge[cur][i]=bridge[i][cur]=true;
}
}
}
vis[cur]=2;
}
int main()
{
freopen("a.txt","r",stdin);
int n,m;
while(cin>>n>>m)
{
memset(edge,0,sizeof(edge));
memset(vis,0,sizeof(vis));
memset(bridge,0,sizeof(bridge));
memset(cut,0,sizeof(cut));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
for(int i=0;i<m;i++)
{
int s,t;
cin>>s>>t;
edge[s][t]=edge[t][s]=1;
}
cut_bridge(0,-1,0,n);
cout<<"bridge:\n";
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(bridge[i][j])
cout<<i<<" "<<j<<endl;
cout<<"cut:\n";
for(int i=0;i<n;i++)
if(cut[i]==1)
cout<<i<<endl;
}
return 0;
}
/*
0
/  \
1    2
/ \  / \
3   4 5  6
*/

• 本文已收录于以下专栏：

举报原因： 您举报文章：割点与桥 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)