【第22期】观点:IT 行业加班,到底有没有价值?

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


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

相关文章推荐

POJ1308

解题思路:这个题目要我们判断所给图是不是树,这里可以把条件转化为:1.图中没有环;2,图中每个节点除了根节点的入度都为1,根节点入度为0; 没有环和入度不大于1,可以更具kruskal的并查集思想判...

并查集之POJ1308

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

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

poj1308 并查集

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

并查集的初级应用及进阶

并查集的初级应用及进阶一、精华精华提炼1:  内容:并查集就是树的孩子表示法的应用。  解释:对于下图所示树,它的孩子表示法为:                                    ...

并查集POJ1308

题目:题目链接  给出一对对的数字a,b,表示从a到b有一条边。判断这是不是一棵树。 多case,每个case以0 0 结尾 输入以-1 -1结尾 解法:  这道题还是很坑爹的,其实网上给的并查集的代...

poj1308 Is It A Tree?

http://poj.org/problem?id=1308 题意:geiy

并查集浅析(poj 1308)

转载一篇讲解,讲的非常透彻:http://blog.csdn.net/pure_life/article/details/2922118 POJ 1308 形成树的条件:(1) 只有一个根 (2)...

Poj1308 Is It A Tree? 并查集

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

POJ1308解题报告

POJ1308解题报告          一道水题,但也有值得我们思考的地方:                 给定一打节点的联通关系,判断它们是否构成了一棵树。         一棵树的涵...

poj1308Is It A Tree?(并查集)

Is It A Tree? Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22637   ...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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