POJ-1308 Is It A Tree?(并查集判断是否是树)
Posted jiamian
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-1308 Is It A Tree?(并查集判断是否是树)相关的知识,希望对你有一定的参考价值。
http://poj.org/problem?id=1308
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.
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、判断是否有唯一的根结点,即不是多棵树
3、空树也是树
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <sstream> 13 const int INF=0x3f3f3f3f; 14 typedef long long LL; 15 const int mod=1e9+7; 16 const int maxn=1e5+10; 17 using namespace std; 18 19 int fa[100010]; 20 vector<int> vt; 21 22 void init(int n) 23 { 24 for(int i=0;i<=n;i++) 25 fa[i]=i; 26 } 27 int Find(int x) 28 { 29 return x==fa[x]? x:fa[x]=Find(fa[x]); 30 } 31 32 int main() 33 { 34 #ifdef DEBUG 35 freopen("sample.txt","r",stdin); 36 #endif 37 // ios_base::sync_with_stdio(false); 38 // cin.tie(NULL); 39 40 int a,b; 41 int flag=1;//判断是否有环,1表示无环 42 init(100005); 43 int T=0;//样例个数 44 while(~scanf("%d %d",&a,&b)&&!(a==-1&&b==-1)) 45 { 46 if(a==0&&b==0)//一个样例结束,判断输出并初始化 47 { 48 int num=0; 49 for(int i=0;i<vt.size();i++) 50 if(vt[i]==fa[vt[i]]) num++; 51 if(flag&&num<=1) printf("Case %d is a tree. ",++T);//num=0为空树 52 else printf("Case %d is not a tree. ",++T); 53 flag=1; 54 vt.clear(); 55 init(100005); 56 continue; 57 } 58 int aa=Find(a); 59 int bb=Find(b); 60 if(aa==bb) flag=0;//有环 61 else if(flag)//无环再操作,已经判断有环就不用再进行了 62 { 63 fa[aa]=bb; 64 vt.push_back(a); 65 vt.push_back(b); 66 } 67 } 68 69 return 0; 70 }
-
以上是关于POJ-1308 Is It A Tree?(并查集判断是否是树)的主要内容,如果未能解决你的问题,请参考以下文章
POJ-1308 Is It A Tree?(并查集判断是否是树)
HDU 1325 POJ 1308 Is It A Tree? (并查集)
HDU-1325&&POJ-1308 Is It A Tree?(基础并查集)