POJ 1144 Network

Network

Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on PKU. Original ID: 1144
64-bit integer IO format: %lld      Java class name: Main
 
 
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

 
解题:求割点个数。。。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 110;
18 int n,cnt,dfn[maxn],low[maxn];
19 vector<int>g[maxn];
20 bool iscut[maxn];
21 void tarjan(int u,int fa) {
22     dfn[u] = low[u] = ++cnt;
23     int v,son = 0;
24     for(v = 0; v < g[u].size(); v++) {
25         if(!dfn[g[u][v]]) {
26             son++;
27             tarjan(g[u][v],u);
28             if(low[u] > low[g[u][v]]) low[u] = low[g[u][v]];
29             if(low[g[u][v]] >= dfn[u]) iscut[u] = true;
30         } else if(g[u][v] != fa && low[u] > dfn[g[u][v]])
31             low[u] = dfn[g[u][v]];
32     }
33     if(fa < 0 && son == 1) iscut[u] = false;
34 }
35 int main() {
36     int u,v,ans;
37     while(scanf("%d",&n),n) {
38         for(int i = 0; i <= n; i++){
39             g[i].clear();
40             dfn[i] = low[i] = 0;
41             iscut[i] = false;
42         }
43         while(scanf("%d",&u),u) {
44             while(scanf("%d",&v)) {
45                 g[u].push_back(v);
46                 g[v].push_back(u);
47                 if(getchar() == \n) break;
48             }
49         }
50         ans = cnt = 0;
51         tarjan(1,-1);
52         for(int i = 1; i <= n; i++)
53             ans += iscut[i];
54         printf("%d\n",ans);
55     }
56     return 0;
57 }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。