求割點

//poj1144

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define M 100100
#define N 1100
using 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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章