POJ1308——并查集

原创 2016年08月29日 12:17:33

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【解题报告】 POJ 1308 Is It A Tree? 并查集判断一棵树

题目连接: POJ 1308 利用并查集判断一棵树,不能存在环(就判断是不是两个元素拥有相同的祖先即可),注意空树也是一棵树。 并查集一开始没有用递归来写,总的代码显得很长。 /* POJ 13...

poj 1308 Is It A Tree?并查集

题目的链接如下:点击打开链接 Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Tota...

POJ1308-并查集

题目:题目链接   题意:给出你每对点的链接情况,问你最后构成的是不是一棵树   分析:并查集,判断是不是有多个根即可(按照树 的定义,只有一个根。每个子节点只能有一个父节点)。对了,没 有节点也算树...

poj1308【并查集】

= =、如果输入的两个数相等。就不是一颗树啊,不能自己指向自己。 水。(瞎开的数组)。//#include #include #include #include #include #include...

POJ 1308 HDU 1358 Is It A Tree? 并查集 (好多WA点)

 A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of ...

POJ 1308-Is It A Tree?(并查集)

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32343   ...

poj 1308(并查集)

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14319   ...

并查集 poj1308 hd1272

poj1308 题目链接题目大意:输入若干组测试数据,输入 (-1 -1) 时输入结束。每组测试数据以输入(0 0)为结束标志。然后根据所给的所有(父亲, 孩子)数据对判断 是否能构成一棵树。分析...

【并查集】hdu 1325 Is It A Tree? 或 poj 1308 Is It A Tree?

/* THE PROGRAM IS MADE BY PYY */ /*-----------------------------------------------------------------...

POJ 1308 Is It A Tree? && NYOJ 129 (树的判定+并查集)

【题目链接】click here~~ 【题目大意】给定多对节点,判断所有节点能否组成一棵树 【解题思路】并查集的基本操作,定义node,edge,统计node和edge的数目,如果(edge==no...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)