POJ1144Network(割边模板题)

Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13771   Accepted: 6257

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

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    
#include  
#include  
#include    
#include    
#include    
#include   
#include  
#include   
#include    
#include   
#include 
#define inf 9999999; 
using namespace std;

/*start:2017/5/7 21:19*/
/*end:2017/5/7 22:30*/

int root, indexx;
int low[105], num[105], ans, n;//low[i]表示i这个点能够到达的最小的时间戳,num[i]表示i这个点的编号
bool flag[105];
vector tu[105];
void dfs(int x, int y)//x为子节点,y为父节点
{
	int child, i, k;
	child = 0;
	indexx++;
	low[x] = indexx;//x这个点能够到达的最小的时间戳就是自己本身
	num[x] = indexx;
	for (i = 0; i < tu[x].size(); i++)
	{
		k = tu[x][i];
		if (num[k] == 0)
		{
			child++;
			dfs(k, x);
			low[x] = min(low[x], low[k]);//若是k这个子节点能够绕过父节点x到达更小的时间戳(时间戳越小,相当于辈分越大)
			//意味着他这个父节点也能到子节点到的这个最小的时间戳
			if (x != root&&low[k] >= num[x])//子节点的时间戳若是大于父节点的编号
				//意味着他只能到比他更小的点,所以这个点可以当作割点
			{
				flag[x] = true;
			}
			if (x == root&&child == 2)//如果当前点为根节点,要有两个儿子,那么这个点也是割点
			{
				flag[x] = true;
			}
		}
		else if (k != y)
		{
			low[x] = min(low[x], num[k]);
		}
	}
}
int main()
{
	for (;;)
	{
		int x, y;
		cin >> n;
		if (n == 0)
			break;
		indexx = 0;
		ans = 0;
		memset(tu, 0, sizeof(tu));
		memset(low, 0, sizeof(low));
		memset(num, 0, sizeof(num));
		memset(flag, false, sizeof(flag));
		for (;;)
		{
			cin >> x;
			if (x == 0)
				break;
			while (getchar() != '\n')
			{
				cin >> y;
				tu[x].push_back(y);
				tu[y].push_back(x);
			}
		}
		root = 1;
		dfs(1, root);
		for (int i = 1; i <= n; i++)
		{
			if (flag[i])
				ans++;
		}
		cout << ans << endl;
	}
}


你可能感兴趣的:(日常总结,ACM,模板题,图)