1. 程式人生 > >poj 1144 Network

poj 1144 Network

always rom rec fin locks 兩個 ring pri std

                        poj 1144——Network
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13995 Accepted: 6371

Description

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 file 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 file 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的工作人員意識到,除非這個地點是不可達的,否則這種情況就會發生,它還會導致一些其它的地點不能互相通訊。在這種情況下我們會稱這個地點(錯誤發生的地方)為critical。現在工作人員想要寫一個程序找到所有critical地點的數量。幫幫他們。

輸入

輸入文件包括若組測試數據。每一組是一個網絡,每一組測試數據的第一行是地點的總數量N<100.每個接下來最多N行包括一個數字表示一個地點和與它相連接的地點的數字。這些最多N行完全描述了整個網絡,比如,網絡中每個直接連接的兩個地點被至少一行包括。一行內的所有數字都要用空格隔開。每組數據需要用單獨的一個0結束。最後的塊只有一行即N=0。

輸出

輸出除了最後一個組其他每一個組的critical地點的數量,每個塊用一行輸出。

思路

這個題差不多就是個tarjan求割點的板子。

就是我們在輸入時處理起來有點麻煩。。。

代碼:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10000
using namespace std;
int x,y,n,tot,ans,tim;
int head[N],low[N],dfn[N],cut_point[N];
struct Edge
{
    int to,next,from;    
}edge[N];
void add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
void begin()
{
    ans=0,tot=1,tim=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(head,0,sizeof(head));
    memset(cut_point,0,sizeof(cut_point));
}
int tarjan(int now,int pre)
{
    int sum=0; bool boo=false;
    dfn[now]=low[now]=++tim;
    for(int i=head[now];i;i=edge[i].next)
    {
        if(i==(pre^1)) continue;
        int t=edge[i].to;
        if(!dfn[t])
        {
            sum++;
            tarjan(t,i);
            low[now]=min(low[now],low[t]);
            if(low[t]>=dfn[now]) boo=true;
            //if(dfn[t]>low[now]) cut_edge[i]=1;    
        }
        else low[now]=min(low[now],dfn[t]);
    }
    if(!pre) 
    {
      if(sum>1) cut_point[now]=1;    
    }//要有大括號!!!!(唉,為了這個錯誤找了3天!!!!!)
    else if(boo) cut_point[now]=1; 
}
int main()
{
    char c;
    while(scanf("%d",&n)!=EOF&&n)
    {
        begin();
        while(scanf("%d",&x)!=EOF&&x)
        {
            while((c=getchar())!=\n)
            {
                scanf("%d",&y);
                add(x,y);add(y,x);
            }
        }
        tarjan(1,0);
        for(int i=1;i<=n;i++)
         if(cut_point[i]) ans++;
        printf("%d\n",ans);
    }
    return 0;
}

poj 1144 Network