求割点

来源:互联网 发布:虚拟专用网软件 编辑:程序博客网 时间:2024/06/13 15:31

//poj1144#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#define M 100100#define N 1100using namespace std;struct Node{    int v,next;}e[M];int head[N],e_num,n;int low[N],dfn[N],stack[N],dfs_num,top,root,d[N];void add_edge( int u, int v){    e[e_num].v=v;    e[e_num].next=head[u];    head[u]=e_num++;}void dfs( int u, int fa ){    low[u]=dfn[u]=++dfs_num;    stack[++top]=u;    int cnt=0;    for( int p=head[u];p!=-1; p=e[p].next )    {        int v=e[p].v;        if(!dfn[e[p].v] )        {            dfs(v,u);            cnt++;            if(low[u]>low[v])low[u]=low[v];            if((u==root&&cnt>1)||(u!=root&&dfn[u]<=low[v]))d[u]=1;        }        else if(v!=fa&&low[u]>dfn[v])low[u]=dfn[v];    }}void solve(){    top=dfs_num=0;    memset(d,0,sizeof(d));    memset(low,0,sizeof(low));    memset(dfn,0,sizeof(dfn));    for( root=1; root<=n; root++ )    {        if(!dfn[root])dfs(root,0);    }    int ans=0;    for( int i=0; i<=n; i++ )ans+=d[i];    printf("%d\n",ans);}int main(){    //freopen("in.txt","r",stdin);    int u,v;    char ch;    while( scanf("%d",&n),n)    {        memset(head,-1,sizeof(head));        e_num=0;        while(scanf("%d%c",&u,&ch),u)        {            while(~scanf("%d%c",&v,&ch))            {                add_edge(u,v);                add_edge(v,u);                if(ch=='\n')break;            }        }        solve();    }    return 0;}

原创粉丝点击