poj1523(割点)


1.思路参考刘汝佳的算法艺术与信息学竞赛。

2.代码。

#include<cstdio>
#include<iostream>
using namespace std;
//   freopen("data.in","r",stdin);

#include<cstring>
#define N 1005
bool G[N][N];
int _V;
int root;
bool vis[N];
int deep[N],ancestor[N];
bool cutP[N];
//int son[N];
int cutE[N][N];
void init()
{
    memset(G,0,sizeof(G));
    memset(cutP,0,sizeof(cutP));
  //  memset(son,0,sizeof(son));
    memset(deep,-1,sizeof(deep));
    memset(vis,0,sizeof(vis));
    _V=2;
    root=1;
}
void dfs(int u,int father,int d)
{

    deep[u]=ancestor[u]=d;
    int son=0;
    for(int v=1; v<=_V; v++)
    {
        if(G[u][v])
        {
            if(deep[v]==-1)
            {
                son++;
                dfs(v,u,d+1);
                ancestor[u]=ancestor[u]>ancestor[v]?ancestor[v]:ancestor[u];
            }
            else if(v!=father)
            {
                ancestor[u]=ancestor[u]>deep[v]?deep[v]:ancestor[u];
            }
        }
    }
    if(u==root)
    {
        if(son>1)cutP[root]=true;
        else cutP[root]=false;
    }
    else if(ancestor[u]>=deep[father])//
    {
        cutP[father]=true;
        cutE[father][u]=true;
    }
}

void output(int Case)
{
    printf("Network #%d\n",Case);
    bool exist=false;

    for(int u=1; u<=_V; u++)
    {
        if(cutP[u])
        {
            exist=true;
            int branch;
            if(u==1)branch=0;
            else branch=1;
            bool f=false;
            for(int v=1;v<=_V;v++)
            {
                if(G[u][v])
                {
                    if(ancestor[v]>deep[u])branch++;
                    if(ancestor[v]==deep[u])f=true;
                }
            }
            printf("  SPF node %d leaves %d subnets\n",u,branch+f);
        }
    }
    if(!exist)printf("  No SPF nodes\n");
    cout<<endl;
}
int main()
{
 //   freopen("data.in","r",stdin);
    int Case=1;
    int u,v;
    while(~scanf("%d",&u)&&u)
    {
        init();
        scanf("%d",&v);
        vis[u]=vis[v]=G[u][v]=G[v][u]=true;

        while(~scanf("%d",&u)&&u)
        {
            scanf("%d",&v);
            G[u][v]=G[v][u]=true;
            if(!vis[u])
            {
                vis[u]=true;
                _V++;
            }
            if(!vis[v])
            {
                vis[v]=true;
                _V++;
            }
        }

        dfs(1,0,0);

        output(Case);
        Case++;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值