POJ 1144 Network

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

Hint

You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.

Source

 
题目是模板题,没有什么好说的,可是四级没过的我,弄懂这个题的输入花了不少功夫.
这个题的输入是.
多组输入,对于每一组
第一行n,表示有n个点.
接下来最多有n行,表示存在的路径,而对于每一行
第一个数字表示起点,该行其余的表示终点
如果在一组内,有一行只有一个0,表示该组数据结束.
如果输入n为0,表示输入全部结束.
 
下面是AC代码:
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 using namespace std;
 5 int root;
 6 int num[1000];
 7 int low[1000],book[1024];
 8 int Num;
 9 int n,ui,flag=0;
10 int u[1000],v[1000];
11 int first[1024],Next[10086];
12 int index,ans;
13 void dfs(int cur,int father)
14 {
15     int child=0;
16     index++;
17     num[cur]=index;
18     low[cur]=index;
19     int k=first[cur];
20     while(k!=-1){
21         if(num[v[k]]==0){
22             child++;
23             dfs(v[k],cur);
24             low[cur]=min(low[cur],low[v[k]]);
25             if(cur!=root&&low[v[k]]>=num[cur]){
26                 if(!book[cur]){ans++;}
27                 book[cur]=1;
28             }
29             if(cur==root&&child>=2){
30                 if(!book[cur]){ans++;}
31                 book[cur]=1;
32             }
33         }
34         else if(v[k]!=father){
35             low[cur]=min(low[cur],num[v[k]]);
36         }
37         k=Next[k];
38     }
39 }
40 
41 int main()
42 {
43     char c;
44     while(cin>>n&&n){
45         memset(num,0,sizeof(num));
46         memset(low,0,sizeof(low));
47         memset(book,0,sizeof(book));
48         ans=index=0;
49         for(int i=1;i<=n;i++){
50             first[i]=-1;
51         }
52         getchar();
53         flag=Num=0;
54         int Count=1;
55         while(~scanf("%c",&c)){
56             if(c!=10&&c!=' '){Num=Num*10+c-48;}
57             else {
58 
59                 if(Num==0&&flag==0){break;}
60                 if(!flag){ui=Num;}
61                 else{u[Count]=ui;v[Count]=Num;Next[Count]=first[ui];first[ui]=Count++;}
62                 if(c==10){flag=0;Num=0;continue;}
63                 Num=0;flag=1;
64             }
65         }
66         Count--;
67         for(int i=1;i<=Count;i++){
68             u[i+Count]=v[i];
69             v[i+Count]=u[i];
70             Next[i+Count]=first[v[i]];
71             first[v[i]]=i+Count;
72         }
73 
74 
75         root=1;
76         dfs(1,root);
77         cout<<ans<<endl;
78 
79 
80     }
81 }
 
原文地址:https://www.cnblogs.com/ZGQblogs/p/8994015.html