Poj 1144
Network
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8403 | Accepted: 3930 |
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.
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;
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
Central Europe 1996
上午终于考试结束咯。这意味着寒假的到来,也是我们ACMer奋斗的最佳时机。
作为图论渣渣,我不得不努力学习咯,不然会被学弟们笑话的 #_#
然后今天果断从头开始学习咯,拿了个割点的一血
模板题,就是求图中割点的数目
上午终于考试结束咯。这意味着寒假的到来,也是我们ACMer奋斗的最佳时机。
作为图论渣渣,我不得不努力学习咯,不然会被学弟们笑话的 #_#
然后今天果断从头开始学习咯,拿了个割点的一血
模板题,就是求图中割点的数目
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <vector> 5 #include <string> 6 #include <sstream> 7 8 using namespace std; 9 10 #define min(x,y) ((x) < (y) ? (x) : (y)) 11 12 struct Edge { 13 int u, v; 14 Edge() {} 15 Edge(int uu, int vv):u(uu), v(vv){} 16 }; 17 18 vector<int> next[105]; 19 vector<Edge> edges; 20 int n, m, pre[105], low[105], iscut[105]; 21 int dfs_clock; 22 23 void init() { 24 for (int i = 1; i <= n; i++) 25 next[i].clear(); 26 27 edges.clear(); 28 memset(pre, 0, sizeof(pre)); 29 memset(iscut, 0, sizeof(iscut)); 30 dfs_clock = 0; 31 } 32 33 void AddEdge(int u, int v) { 34 edges.push_back(Edge(u, v)); 35 next[u].push_back(edges.size() - 1); 36 } 37 38 int dfs(int u, int fa) { 39 int lowu = pre[u] = ++dfs_clock; 40 int child = 0; 41 for (int i = 0; i < next[u].size(); i++) { 42 Edge e = edges[next[u][i]]; 43 int v = e.v; 44 if ( !pre[v] ) { 45 child++; 46 int lowv = dfs(v, u); 47 lowu = min(lowu, lowv); 48 if ( lowv >= pre[u] ) 49 iscut[u] = 1; 50 } 51 else if ( pre[v] < pre[u] && v != fa ) 52 lowu = min(lowu, pre[v]); 53 } 54 55 if (fa < 0 && child == 1) 56 iscut[u] = 0; 57 58 low[u] = lowu; 59 60 return lowu; 61 } 62 63 int main(void) { 64 while (scanf("%d", &n) && n) { 65 66 init(); 67 68 string str; 69 while (getline(cin, str) && str[0] != '0') { 70 { 71 istringstream sin(str); 72 sin >> m; 73 int v; 74 while (sin >> v) { 75 AddEdge(m, v); 76 AddEdge(v, m); 77 } 78 } 79 } 80 81 dfs(1, -1); 82 int ans = 0; 83 for (int i = 1; i <= n; i++) { 84 ans += (iscut[i] ? 1 : 0); 85 } 86 87 printf("%d\n", ans); 88 } 89 90 return 0; 91 }
posted on 2014-01-11 13:40 Stomach_ache 阅读(116) 评论(0) 编辑 收藏 举报