poj 1523 SPF

求割点和块的个数。

相关定理及证明,大白书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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值