tarjan求割點HOJ1007SPF

來源:互聯網
上載者:User
//求割點HOJ1007#include <cstdio>#define min(a,b)  a>b?b:a;#define max(a,b)  a>b?a:b;const int V=1001;struct Edge{    int next,to;} edge[V*V];int cnt,idx;int cut[V],n,head[V];int low[V],dfn[V];void init(){    cnt=0;    idx=0;    for(int i=1; i<=V; i++)    {        head[i]=-1;        low[i]=0;        dfn[i]=0;        cut[i]=0;    }}void addedge(int u,int v){    edge[cnt].to=v;    edge[cnt].next=head[u];    head[u]=cnt++;}void tarjan(int u,int fa){    low[u]=dfn[u]=(++idx);    for(int k=head[u]; k!=-1; k=edge[k].next)    {        int v=edge[k].to;        if(v==fa)  continue;        if(dfn[v]!=0)        {            low[u]=min(dfn[v],low[u]);            continue;        }        tarjan(v,u);        low[u]=min(low[u],low[v]);        if(low[v]>=dfn[u])  cut[u]++;//訪問不到比U小的節點    }    if(fa!=0) cut[u]++;}int main(){    //freopen("a.txt","r",stdin);    int a,b;    int num=0;    while(scanf("%d",&a)!=EOF)    {        if(a==0)  break;        init();        n=0;        scanf("%d",&b);        n=max(a,n);        n=max(b,n);        addedge(a,b);        addedge(b,a);        while(1)        {            scanf("%d",&a);            if(a==0) break;            scanf("%d",&b);            n=max(a,n);            n=max(b,n);            addedge(a,b);            addedge(b,a);        }        tarjan(1,0);        bool flag=false;        if(num>0)  printf("\n");        printf("Network #%d\n",++num);        for(int i=1; i<=n; i++)            if(cut[i]>1)            {                printf("  SPF node %d leaves %d subnets\n",i,cut[i]);                flag=true;            }        if(!flag)  printf("  No SPF nodes\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.