Network
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 7354 | Accepted: 3458 |
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
如果u为割点,当且仅当满足下面的其中一个条件
1、如果u为树根,那么u必须有多于1棵子树
2、如果u不为树根,那么(u,v)为树枝边,当low[v]>=dfsNum[u]时。
View Code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define MAX 105 6 7 using namespace std; 8 9 struct node 10 { 11 int to,next; 12 }; 13 14 node edge[MAX*MAX]; 15 int head[MAX]; 16 int idx; 17 18 int N,time; 19 int Gpoint[MAX],root; 20 int dfsNum[MAX],low[MAX]; 21 22 void addNode(int from,int to) 23 { 24 edge[idx].to = to; 25 edge[idx].next = head[from]; 26 head[from] = idx ++; 27 } 28 29 void tarjan(int cur,int fat) 30 { 31 dfsNum[cur] = low[cur] = ++ time; 32 for(int i=head[cur]; i!=-1; i=edge[i].next) 33 { 34 int to = edge[i].to; 35 if(!dfsNum[to]) 36 { 37 tarjan(to,cur); 38 low[cur] = min(low[cur],low[to]); 39 if(low[to] >= dfsNum[cur]) 40 { 41 if(cur != fat) 42 Gpoint[cur] ++; 43 else 44 root ++; 45 } 46 } 47 else 48 low[cur] = min(low[cur],dfsNum[to]); 49 } 50 return; 51 } 52 53 void solve() 54 { 55 int ret = 0; 56 memset(Gpoint,0,sizeof(Gpoint)); 57 memset(dfsNum,0,sizeof(dfsNum)); 58 time = root = 0; 59 tarjan(1,1); 60 if(root > 1) 61 ret ++; 62 for(int i=2; i<=N; i++) 63 if(Gpoint[i]) 64 ret ++; 65 printf("%d\n",ret); 66 } 67 68 int main() 69 { 70 while(~scanf("%d",&N) && N) 71 { 72 idx = 0; 73 memset(head,-1,sizeof(head)); 74 int temp; 75 while(scanf("%d",&temp) && temp) 76 { 77 while(getchar()!='\n') 78 { 79 int t; 80 scanf("%d",&t); 81 addNode(temp,t); 82 addNode(t,temp); 83 } 84 } 85 solve(); 86 } 87 return 0; 88 }
本文介绍了一种用于检测网络中割点的算法,并提供了一个具体的实现案例。割点是指网络中一旦发生故障会导致整个网络连通性受损的关键节点。通过递归深度优先搜索,算法能够找出所有这样的关键节点。

1183

被折叠的 条评论
为什么被折叠?



