zoj——1311 Network

來源:互聯網
上載者:User

標籤:oge   and   電源   tle   failure   ssi   scanf   sam   ges   

NetworkTime Limit: 2 Seconds      Memory Limit: 65536 KB

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.


Input

The input consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0.


Output

The output contains for each block except the last in the input one line containing the number of critical places.


Sample Input

5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0


Sample Output

1
2

 

題意:

電話公司(TLC)正在建立一個新的電話電纜網路。它們串連數個整數,從1到n,沒有兩個地方有相同的數字。這些線是雙向的,總是串連在一起的兩個地方,在每一個地方的電話線結束在交換器。每一個地方都有一個電話交換器。從每一個地方都可以通過其他地方的線,但它不需要直接連接,它可以通過幾個交換。有時電源在一個地方故障,然後交換不工作。TLC的官員意識到,在這種情況下,除了失敗的地方是不可到達的之外,也可能導致其他地方不能互相串連。在這種情況下,我們會說發生故障的地方是關鍵的。現在,官員們正試圖編寫一個程式來找出所有這些關鍵地點的數量。協助他們。

思路:

裸題,tarjan求割點的數量

但是輸入的時候特別噁心、、、

代碼:

 

#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#define N 10010using namespace std;char ch[N];int n,m,x,y,tim,tot,ans;int dfn[N],low[N],vis[N],head[N],cut_point[N];struct Edge{    int from,to,next;}edge[N];int read(){    int x=0,f=1; char ch=getchar();    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();}    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();}    return x*f;}void add(int x,int y){    tot++;    edge[tot].to=y;    edge[tot].next=head[x];    head[x]=tot;}void clean(){    ans=0,tim=0;tot=1;    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(vis,0,sizeof(vis));    memset(head,0,sizeof(head));    memset(cut_point,0,sizeof(cut_point));}void tarjan(int now,int pre){    int sum=0; bool boo=false; vis[now]=true;    dfn[now]=low[now]=++tim;    for(int i=head[now];i;i=edge[i].next)    {        int t=edge[i].to;        if((pre^1)==i) continue;        if(!dfn[t])        {            sum++;tarjan(t,i);            low[now]=min(low[now],low[t]);            if(low[t]>=dfn[now]) boo=true;        }        else low[now]=min(low[now],dfn[t]);    }    if(!pre) {if(sum>1) ans++;}    else if(boo) ans++;}int main(){    while(1)    {        n=read();if(n==0) break;        clean();        while(scanf("%d",&x)&&x)        {            while(getchar()!=‘\n‘)            {                scanf("%d",&y);                add(x,y),add(y,x);            }        }        for(int i=1;i<=n;i++)         if(!dfn[i]) tarjan(i,0);        printf("%d\n",ans);    }    return 0;}

突然發現自己好zz啊,老是把東西寫反、、、

 

zoj——1311 Network

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.