您的位置:首页 > 其它

【模板】图的割点

2016-04-30 12:47 357 查看
lrj

dfn:发现时间

low:子树中可以追溯到最早的节点的发现时间

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=501000;
const int N=101000;
struct E {int to,nxt;}edge[M*2];
int low
,dfn
,Time=0;
int idx
,tot=1;
bool iscut
;
int n,m,ans;
void addedge(int from,int to){
edge[tot].to=to;edge[tot].nxt=idx[from];idx[from]=tot++;
}
void dfs(int x,int fa){
int child=0;
dfn[x]=low[x]=++Time;
for(int t=idx[x];t;t=edge[t].nxt){
E e=edge[t];
if(!dfn[e.to]){
child++;
dfs(e.to,x);
low[x]=min(low[e.to],low[x]);
if(low[e.to]>=dfn[x]) iscut[x]=1;
}
else if(e.to!=fa)
low[x]=min(low[x],dfn[e.to]);
}
if(fa==-1 && child==1) iscut[x]=0;
}
int main(){
//  freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);addedge(y,x);
}
dfs(1,-1);
for(int i=1;i<=n;i++) if(iscut[i]) ans++;
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: