poj1144-Network(求割点个数)

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

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

翻译:
.一家电话线公司正在建立一个新的电话电缆网。它们连接几个由1到N的整数编号的地方。没有两个地方的号码是一样的。线路是双向的,总是连接在一起的两个地方和在每个地方的线路结束在一个电话交换机。每个地方都有一个电话交换机。From each place it is
可以通过线路到达其他每一个地方,但不需要直接连接,它可以通过几个交换机。有时某个地方的电源出现故障,然后交换机就不工作了。TLC的官员们意识到,在这种情况下,除了无法到达失败的地方之外,还可能导致其他一些地方无法相互连接。在这种情况下,我们会说失败的地方
发生)是关键的。现在,官员们正试图编写一个程序,找出所有这些关键地点的数量。帮助他们。
输入
输入文件由若干行组成。每个块描述一个网络。在每个块的第一行中有N<100的位置数。下一行最多N行中的每一行包含一个位置的编号,后跟从该位置有一条直线的某些位置的编号。这些至多N行完全描述了网络,即网络中两个位置的每个直接连接至少包含在一行中。一行中的所有数字都是分开的
一个空间。每个块以仅包含0的行结束。最后一个块只有一行N=0;
输出
除了输入文件中的最后一个块外,每个块的输出都包含一行,其中包含关键位置的数量。

解析:
典型的求割点题,需要注意的是数据的输入;
割点:在一个无向连通图中,如果删除某个顶点后图不再连通,我们称这样的点为割点;

代码:

#include<stdio.h>
#include<string.h>
int e[1100][1100],num[1100],low[1100],flag[1100],index,m,n,root;
int minn(int a,int b)
{
	return a<b?a:b;
}
void dfs(int cur,int father)//传入的两个参数依次为当前编号和父节点编号
{
	int child=0,i;
	index++;//时间戳
	num[cur]=index;//当前顶点的时间戳
	low[cur]=index;//当前顶点能够访问到的最早顶点的时间戳,初始时为自己
	for(i=1;i<=n;i++)
	{
		if(e[cur][i]==1)
		{
			if(num[i]==0)//顶点i还没被访问过,此时i为cur的儿子
			{
				child++;//cur儿子数加一
				dfs(i,cur);//继续往下深搜
				low[cur]=minn(low[cur],low[i]);//更新当前顶点能访问的最早顶点的时间戳
				if(cur!=root&&low[i]>=num[cur])//若当前顶点不是根节点且满足low[i]>=num[cue],则为割点
				flag[cur]=1;
				if(cur==root&&child==2)//若是根节点,若有两个儿子,则为割点
				flag[cur]=1;
			}
			else if(i!=father)//顶点i被访问过,且这个顶点不是当前顶点的父亲,那么i为当前顶点的祖先,更新当前顶点能访问到的最早时间戳
			{
				low[cur]=minn(low[cur],num[i]);
			}
		}
	}
	return;
}
int main()
{
	int i,j,s;
	while(scanf("%d",&n)&&n!=0)
	{
		memset(num,0,sizeof(num));
		memset(low,0,sizeof(low));
		memset(flag,0,sizeof(flag));
		for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		e[i][j]=0;
		s=0;
		while(scanf("%d",&m)&&m!=0)
		{
			while(getchar()!='\n')
			{
				int v;
				scanf("%d",&v);
				e[m][v]=1;
				e[v][m]=1;
			}
		}
		root=1;
		dfs(1,root);//从1号顶点开始深度优先遍历
		for(i=1;i<=n;i++)
		if(flag[i]==1)
		s++;
		printf("%d\n",s);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值