72 73 76 89 82 84 89 81

我们都有梦想,凭什么不努力

POJ1308——并查集

1.题目:

Is It A Tree?
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29735   Accepted: 10173

Description

A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. 

There is exactly one node, called the root, to which no directed edges point. 
Every node except the root has exactly one edge pointing to it. 
There is a unique sequence of directed edges from the root to each node. 
For example, consider the illustrations below, in which nodes are represented by circles and edges are represented by lines with arrowheads. The first two of these are trees, but the last is not. 

In this problem you will be given several descriptions of collections of nodes connected by directed edges. For each of these you are to determine if the collection satisfies the definition of a tree or not.

Input

The input will consist of a sequence of descriptions (test cases) followed by a pair of negative integers. Each test case will consist of a sequence of edge descriptions followed by a pair of zeroes Each edge description will consist of a pair of integers; the first integer identifies the node from which the edge begins, and the second integer identifies the node to which the edge is directed. Node numbers will always be greater than zero.

Output

For each test case display the line "Case k is a tree." or the line "Case k is not a tree.", where k corresponds to the test case number (they are sequentially numbered starting with 1).

Sample Input

6 8  5 3  5 2  6 4
5 6  0 0

8 1  7 3  6 2  8 9  7 5
7 4  7 8  7 6  0 0

3 8  6 8  6 4
5 3  5 6  5 2  0 0
-1 -1

Sample Output

Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.
2.题目大意:我们给你小于100个数的集合,然后我们让你判断我们输入的这些边集是否构成一棵树

3.思路:

简单并查集:

我们开辟并查集之后,首先要用记录数组记录哪些点出现过,我们根据出现过的点在进行计数操作,森林是不成立的

其次,我们要知道空时也算一棵树

最后,我们一旦出现练得两个节点出现在一个集合中的话,那么我们在进行连边操纵必然导致环路的生成,就不是树了

就这三点记住就好

4.AC代码 0Ms水过

#include"iostream"
#include"cstdio"
#include"cstdlib"
#include"cstring"
#define N 100

using namespace std;

int fa[N];
int deep[N];
int n;
int x,y; 
bool flag=0;
bool kright=0;
bool book[N];

void init()
{
	for(int i=1;i<N;i++)
	{
		fa[i]=i;
		deep[i]=1;
	}
	memset(book,0,sizeof(book));
}

int find(int x)
{
	if(x==fa[x]) return x;
	else return fa[x]=find(fa[x]);
}

void unit(int x,int y)
{
	x=find(x);
	y=find(y);
	if(x==y) return ;
	else
	{
		if(deep[x]>deep[y]) fa[y]=x;
		else
		{
			if(deep[x]==deep[y]) deep[y]++;
			fa[x]=y; 
		}
	}
}

bool same(int x,int y)
{
	return find(x)==find(y); 
}

int main()
{
	int t=0;
	while(1)
	{
		kright=true; 
		t++;
		init();
		while(scanf("%d%d",&x,&y)&&x!=0&&y!=0)
		{
			if(x==-1&&y==-1)
			{
				flag=1;
				break;
			}
			if(find(x)==find(y)) kright=0;
			unit(x,y);
			book[x]=book[y]=1;
		} 
		if(flag==1) break;
		int sum=0;
		for(int i=1;i<N;i++)
		{
			if(fa[i]==i&&book[i]==1) sum++;
		}
		if(sum!=1&&sum!=0) kright=0;
		if(kright) printf("Case %d is a tree.\n",t);
		else printf("Case %d is not a tree.\n",t);
	}
	return 0;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ltyqljhwcm/article/details/52351913
个人分类: ACMer's collection
所属专栏: POJ 专项题解
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

POJ1308——并查集

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭