码迷,mamicode.com
首页 > 其他好文 > 详细

poj1523(割点)

时间:2016-05-07 09:25:18      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

这道题是求哪个点是割点,并且能把原图分成几块,add_block数组是求增加的所以还需要加1,然后就是我一直在PE,后来发现需要每输出一组后,有一个空行

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int MAXN=1005;
const int MAXM=0x3f3f3f;
struct edge
{
    int to,next;
    bool cut;
} edge[MAXM];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],Stack[MAXN];
int Index,top;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];
int bridge;
void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    edge[tot].cut=false;
    head[u]=tot++;
}
void tarjan(int u,int pre)
{
    int v;
    low[u]=dfn[u]=++Index;
    Stack[top++]=u;
    Instack[u]=true;
    int son=0;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        v=edge[i].to;
        if(v==pre)
            continue;
        if(!dfn[v])
        {
            son++;
            tarjan(v,u);
            if(low[u]>low[v])
                low[u]=low[v];
            if(u!=pre&&low[v]>=dfn[u])
            {
                cut[u]=true;
                add_block[u]++;
            }
        }
        else if(low[u]>dfn[v])
            low[u]=dfn[v];
    }
    if(u==pre&&son>1)
        cut[u]=true;
    if(u==pre)
        add_block[u]=son-1;
    Instack[u]=false;
    top--;
}
void init()
{
    memset(head,-1,sizeof(head));
    memset(cut,0,sizeof(cut));
    memset(Instack,0,sizeof(Instack));
    memset(add_block,0,sizeof(add_block));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
}
int a,b,n;
int main()
{
    int cases=1;
    while(cin>>a)
    {
        init();
        if(a!=0)
        {
            cin>>b;
            addedge(a,b);
            addedge(b,a);
        }
        else if(a==0)
        break;
        while(1)
        {
            cin>>a;
            if(a!=0)
            {
                cin>>b;
                addedge(a,b);
                addedge(b,a);
            }
            else if(a==0)
                break;
        }
        tarjan(1,1);
        bool flag=0;
        cout<<"Network #"<<cases++<<endl;
        for(int i=1; i<=1005; i++)
        {
            if(cut[i])
            {
                cout<<"  SPF node "<<i<<" leaves "<<add_block[i]+1<<" subnets"<<endl;
                flag=1;
            }
        }
        if(flag==0)
            cout<<"  No SPF nodes"<<endl;
             cout<<endl;

    }
    return 0;
}

poj1523(割点)

标签:

原文地址:http://blog.csdn.net/zcmartin2014214283/article/details/51331686

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!