Description:
A telephone line company (TLC) is establishing a new telephone cable network. They is connecting several places numbered by integers from 1 toN. No. Places has the same number. The lines is bidirectional and always connect together both places and in each place the lines end in a telephone exchange . There is one telephone exchange in each place. From each place it's possible to reach through lines every other place, however it need isn't be a direct connection, it CA n go through several exchanges. From time to time the power supply fails in a place and then the exchange does not operate. The officials from TLC realized so in such a case it can happen that besides the fact that the place with the failure is Unreachable, this can also cause the some other places cannot connect to each of the other. In such a case we'll say the place (where the failure occured) is critical. Now the officials is 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. 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 t Here are a direct line from the this place. These at the very N lines completely describe the network, i.e., each direct connection of both places in the network is contained at least in one row. All numbers on one line is separated by one space. Each block ends with a line containing just 0. The last block had 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:
55 1 2 3 4062 1 35 4 6 200
Sample Output:
12
Test instructions: Telephone lines are connected, if some contacts have failed, may be the whole system or interoperability, may also cause some places can not communicate, now need to judge a telephone system there are several points can not be faulted (that is, connected graph for cutting point)
cut point:
A vertex u is a cut point, when and only if satisfied (1) or (2)
(1) U is a root, and U has more than one subtree.
(2) U is not a root, and satisfies the presence (U,V) as a branch edge (or parent-child edge, that is, U is the father of V in the search tree), making DFN (U) <=low (v).
(That is, V has no way to bypass the U-point to reach a smaller point than dfn[u])
Note: The tree mentioned here refers to the search tree under the DFS
#include <stdio.h>#include<algorithm>#include<vector>#include<string.h>#defineN 110using namespacestd;intDfn[n], low[n];intF[n], vis[n];//the F array represents the parent node, and the VIS array marks whether the point is a cut pointintN, Time;vector<vector<int> >G;voidInit () {g.clear (); G.resize (n+1); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); Memset (F,0,sizeof(f));//Initialize the parent node to 0memset (Vis,0,sizeof(VIS)); time=0;}voidTarjan (intUintFA) { intI, Len, V; Dfn[u]= Low[u] = + +Time ; F[u]=FA; Len=g[u].size (); for(i =0; i < Len; i++) {v=G[u][i]; if(!Dfn[v]) {Tarjan (V, u); Low[u]=min (Low[u], low[v]); } Else if(v! = FA) Low[u] = min (Low[u], dfn[v]);//note here that it cannot be written as Low[v], if written as low[v], means that you can cross that point to a point earlier than that point, in fact not necessarily }}intMain () {intA, B, son, ans, I, ni; Charch; while(SCANF ("%d", &N), N) {Init (); Son= ans =0; while(SCANF ("%d", &a), a) { while(SCANF ("%d%c", &b, &ch)! =EOF) {G[a].push_back (b); G[b].push_back (a); if(ch = ='\ n') Break; }} Tarjan (1,0); for(i =2; I <= N; i++) {ni=F[i]; if(Ni = =1) son++; Else if(Dfn[ni] <= low[i]) Vis[ni] =1; } for(i =2; I <= N; i++) if(Vis[i]) ans++; if(Son >1) ans++;//if son>1, the root node is represented by more than 1 child nodes, and the root node is the cut pointprintf"%d\n", ans); } return 0;}
UVA 315 Network