【模板】图的割点

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[N],dfn[N],Time=0;
int idx[N],tot=1;
bool iscut[N];
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;
}

你可能感兴趣的:(模板,图的割点)