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

#### 并查集POJ1308

2015-09-11 08:49:28

#### 并查集之POJ1308

2013-09-27 08:11:07

#### poj1308(并查集判环)

2016-04-28 17:48:55

#### POJ1308 Is It A Tree?(树，并查集)

2017-03-06 12:30:45

#### poj1308

2015-05-10 00:07:37

#### HDU1325 &&poj1308 基础并查集

2014-08-05 10:20:25

#### poj1308【并查集】

2016-07-19 09:39:34

#### poj1308：Is It A Tree?

2012-04-04 20:25:25

#### POJ1308解题报告

2014-11-05 16:26:17

#### POJ 1308

2016-04-02 22:07:28

POJ1308——并查集