您的位置:首页 > 其它

poj 1523 SPF

2015-06-12 18:02 330 查看
求割点和块的个数。

相关定理及证明,大白书P313

#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
int head[1005],cnt,p[1005],ans[1005],dfs_clock;
struct E{
    int to,next;
}edge[2000];
inline void add(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int dfs(int u,int fa)
{
    int lowu=p[u]=++dfs_clock,child=0;
    for(int i=head[u];~i;i=edge[i].next)
    {
        if(!p[edge[i].to])
        {
            ++child;
            int lowv=dfs(edge[i].to,u);
            lowu=min(lowu,lowv);
            if(lowv>=p[u])
                ++ans[u];
        }
        else if(p[edge[i].to]<p[u]&&edge[i].to!=fa) lowu=min(lowu,p[edge[i].to]);
    }
    return lowu;
}
int main()
{
    int i,n,x,y,ca=1;
    while(~scanf("%d",&x)&&x)
    {
        cnt=dfs_clock=0;
        memset(head,-1,sizeof(head));
        memset(p,0,sizeof(p));
        scanf("%d",&y);
        add(x,y);
        add(y,x);
        n=max(x,y);
        while(scanf("%d",&x)&&x)
        {
            scanf("%d",&y);
            add(x,y);
            add(y,x);
            n=max(n,max(x,y));
        }
        printf("Network #%d\n",ca++);
        for(i=1;i<=n;++i) ans[i]=1;
        ans[1]=0;
        dfs(1,-1);
        bool flag=0;
        for(i=1;i<=n;++i)
            if(ans[i]>1)
            {
                flag=1;
                printf("  SPF node %d leaves %d subnets\n",i,ans[i]);
            }
        if(!flag) puts("  No SPF nodes");
        puts("");
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: