Network (poj1144)

发布时间:2017-6-23 13:18:03 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Network (poj1144) ",主要涉及到Network (poj1144) 方面的内容,对于Network (poj1144) 感兴趣的同学可以参考一下。

Network (poj1144)

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.
无向图求割顶;
模板题;
 1 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<math.h>
 6 #include<stdlib.h>
 7 #include<stack>
 8 #include<stdio.h>
 9 #include<ctype.h>
10 #include<map>
11 #include<vector>
12 using namespace std;
13 vector<int>vec[1000];
14 char ans[10000];
15 bool flag[10000];
16 int pre[1000];
17 int low[1000];
18 int tr[1000];
19 int sizee = 0;
20 int dfs(int u,int fa);
21 int main(void)
22 {
23     int n;
24     while(scanf("%d",&n),n!=0)
25     {
26         sizee = 0;
27         int t;
28         memset(flag,0,sizeof(flag));
29         memset(pre,0,sizeof(pre));
30         memset(low,0,sizeof(low));
31         memset(tr,0,sizeof(tr));
32         for(int i = 0; i < 1000; i++)
33             vec[i].clear();
34         while(scanf("%d",&t),t!=0)
35         {
36             int i,j;
37             int id;
38             gets(ans);
39             int  l = strlen(ans);
40             int sum = 0;
41             for(i = 0; i <= l; )
42             {
43                 if(ans[i]>='0'&&ans[i]<='9')
44                 {
45                     sum = 0;
46                     for(j = i; ans[j]!=' '&&ans[j]!='\0'&&j <= l; j++)
47                     {
48                         sum = sum*10;
49                         sum+=ans[j]-'0';
50                     }
51                     i = j;
52                     vec[t].push_back(sum);
53                     vec[sum].push_back(t);
54                 }
55                 else i++;
56             }
57         }
58         dfs(1,-1);
59         int sum = 0;
60         for(int i = 1; i <= n; i++)
61         {
62             sum+=tr[i];
63         }
64         printf("%d\n",sum);
65     }
66     return 0;
67 }
68 int dfs(int u,int fa)
69 {
70     pre[u] = low[u] = ++sizee;
71     int child = 0;
72     for(int i = 0; i < vec[u].size(); i++)
73     {
74         int ic  = vec[u][i];
75         if(!pre[ic])
76         {
77             child++;
78             int lowv = dfs(ic,u);
79             low[u] = min(low[u],lowv);
80             if(lowv >= pre[u])
81             {
82                 tr[u] = 1;
83             }
84         }
85         else if(pre[ic] < pre[u]&&ic!=fa)
86         {
87             low[u] = min(low[u],pre[ic]);
88         }
89     }
90     if(fa < 0&& child == 1)tr[u] = 0;
91     return low[u];
92 }

上一篇:Leetcode: Range Addition
下一篇:filter()函数

相关文章

关键词: Network (poj1144)

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。