POJ1144-Network(割顶模板题)

来源:互联网 发布:ubuntu node 升级 编辑:程序博客网 时间:2024/05/16 23:47
Network
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9134 Accepted: 4292

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

55 1 2 3 4062 1 35 4 6 200

Sample Output

12

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
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;const int MAXN=105;//500;const int MAXM=5050;//0500;int first[MAXN],low[MAXN],dfn[MAXN],inq[MAXN],cut[MAXN];int next[MAXM],u[MAXM],v[MAXM];int cnt,scnt,rt,rt_son;int MIN(int a,int b){return a>b?b:a;}void dfs(int cur){int e;dfn[cur]=low[cur]=++scnt;inq[cur]=1;for(e=first[cur];e!=-1;e=next[e])if(!inq[v[e]])//white{dfs(v[e]);if(cur==rt)rt_son++;else {low[cur]=MIN(low[cur],low[v[e]]);if(low[v[e]]>=dfn[cur])cut[cur]=1;}}  else if(dfn[v[e]]<low[cur])low[cur]=dfn[v[e]];}void add_ded(int a,int b){u[cnt]=a;v[cnt]=b;next[cnt]=first[a];first[a]=cnt++;}int main(){//freopen("123.txt","r",stdin);int n,m,i,s,t;while(~scanf("%d",&n),n){cnt=0;scnt=0;memset(first,-1,sizeof first);memset(next,-1,sizeof next);while(scanf("%d",&s),s){while(getchar()!='\n'){scanf("%d",&t);add_ded(s,t);add_ded(t,s);}}rt=1;rt_son=0;memset(inq,0,sizeof inq);memset(cut,0,sizeof cut);dfs(1);if(rt_son>1)cut[rt]=1;int sum=0;for(i=1;i<=n;i++)sum+=cut[i];printf("%d\n",sum);}return 0;}


0 0
原创粉丝点击