POJ 1308 Is It A Tree? - 并查集

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.


首先,先明确题意:
以下情况不是树:
1.可以由不同路径到达一个结点的;
2.结点之间形成环的。eg 1 2 2 3 3 4 4 1 0 0
3.根结点不止一个的。
4.重复输入的。eg 1 2 1 2 0 0
5.0 0也是一棵树

好的,现在知道题意了,运用并查集的相关知识就可以解决了,详解见注释~

源代码:
 1 #include <iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 int father[50000];
 7 int re[50000];
 8 int i,tree;
10 
11 int find(int a){
12    int fa=father[a];
13    if(a!=father[a]){
14     father[a]=find(father[fa]); //注意递归的使用,表示我不会写递归啊有木有 
15     return father[fa];
16    }
17    else return fa;
18 }
19 
20 void link(int a,int b){
21     int x=find(a);
22     int y=find(b);
23     re[i++]=x; //记录下每次的根节点
24     if(y!=b||y==x){tree=0;return;}//如果y!=b说明之前b有不等于自己的父亲节点,所以现在b不止有一个父亲节点 y==x说明形成环
25     else father[b]=x;
26 }
27 
28 int main()
29 { int count=1;
30  int temp_x,temp_y;
31  while(scanf("%d %d",&temp_x,&temp_y))
32  {
33      if(temp_x+temp_y<0) break;
34        for(int k=0;k<50005;k++)
35          father[k]=k;  //父亲结点初始化为自己
36 i=0;
37      int fa=temp_x;
38      int son=temp_y;
39      int ok=1;
40       tree=1;
41      while(1)
42      {   if(ok==0){scanf("%d %d",&fa,&son);}
43          if(fa+son==0){break;}
44           link(fa,son);
47           ok=0;
48      }
49       int root=find(re[0]);
50          for(int j=1;j<i;j++)
51        if(find(re[j])!=root) //查询之前记录的每个根节点的最终根结点是否为同一个,因为根结点在之后输入的数据中可能会更新
52          { tree=0;break;}
53 
54 
55      if(tree==0)printf("Case %d is not a tree.\n",count++);
56      else printf("Case %d is a tree.\n",count++);
57   }
58     return 0;
59 }

 

 

posted @ 2013-08-05 21:13  小の泽  阅读(187)  评论(0编辑  收藏  举报