zoj--1311 Network

Source: Internet
Author: User

Network time limit: 2 Seconds Memory Limit: 65536 KB

A telephone line company (TLC) is establishing a new telephone cable Network. They is connecting several places numbered by integers from 1 to N. 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 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 there are a Direct Line from this place. These at the very N lines completely describe the network, i.e., each direct connection of both places in the network is Contai Ned at the 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 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

Test instructions

The telephone company (TLC) is building a new telephone cable Network. They connect several integers, from 1 to n, and no two places have the same number. These lines are bidirectional, always connected together in two places, at the end of the telephone line in each place on the Switch. Every place has a telephone switch. From every place can be through other places of the line, but it does not need direct connection, it can be exchanged through Several. Sometimes the power supply fails in one place, then the exchange does not Work. TLC officials realized that in this case, except where the failure was not reachable, it could also lead to the inability of other places to connect with each Other. In this case, we would say that the fault place is Critical. now, officials are trying to write a program to find out the number of all these critical locations. Help Them.

Ideas:

Bare question, Tarjan The number of cut points

But the input is particularly disgusting 、、、

Code:

#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#defineN 10010using namespacestd;Charch[n];intn,m,x,y,tim,tot,ans;intdfn[n],low[n],vis[n],head[n],cut_point[n];structedge{int  from, to,next;} edge[n];intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*f;}voidAddintXintY) {tot++; Edge[tot].to=y; Edge[tot].next=head[x]; head[x]=tot;}voidclean () {ans=0, tim=0; tot=1; Memset (dfn,0,sizeof(dfn)); Memset (low,0,sizeof(low)); Memset (vis,0,sizeof(vis)); Memset (head,0,sizeof(head)); Memset (cut_point,0,sizeof(cut_point));}voidTarjan (intNowintPre) {    intsum=0;BOOLboo=false; vis[now]=true; dfn[now]=low[now]=++tim;  for(intI=head[now];i;i=Edge[i].next) {        intt=edge[i].to; if((pre^1) ==i)Continue; if(!dfn[t]) {sum++; Tarjan (t,i); Low[now] =min (low[now],low[t]); if(low[t]>=dfn[now]) boo=true; }        Else low[now]= min (low[now],dfn[t]); }    if(!pre) {if(sum>1) ans++;} Else if(boo) ans++;}intmain () { while(1) {n=read ();if(n==0) break;        Clean ();  while(SCANF ("%d", &x) &&X) { while(getchar ()! ='\ n') {scanf ("%d",&y);            Add (x, y), Add (y,x); }        }         for(intI=1; i<=n;i++)         if(!dfn[i]) Tarjan (i,0); printf ("%d\n", ans); }    return 0;}

Suddenly found himself good zz ah, always write things anti-、、、

zoj--1311 Network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.