HDU1325--Is It A Tree?
Is It A Tree?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10139 Accepted Submission(s): 2323
Problem 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.
Source
North Central North America 1997
Recommend
Ignatius.L
与小希的迷宫相似,有向连通图,直接用树的定义也可以过。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<iostream> 5 #include<cmath> 6 #include<map> 7 using namespace std; 8 9 int parent[100001]; 10 int in[100001]; 11 int n,m; 12 13 struct Node 14 { 15 int u,v; 16 }fnode[100001]; 17 18 void Ufset() 19 { 20 int i; 21 for(i=1;i<=100000;i++) 22 { 23 parent[i]=-1; 24 } 25 } 26 27 int Find(int x) 28 { 29 int s; 30 for(s=x;parent[s]>=0;s=parent[s]){} 31 while(s!=x) 32 { 33 int temp=parent[x]; 34 parent[x]=s; 35 x=temp; 36 } 37 return s; 38 } 39 40 void Union(int R1,int R2) 41 { 42 int r1=Find(R1),r2=Find(R2); 43 int temp=parent[r1]+parent[r2]; 44 parent[r1]=r2; 45 parent[r2]=temp; 46 } 47 48 int main() 49 { 50 int u,v; 51 int cas=1; 52 while(scanf("%d%d",&u,&v)!=EOF) 53 { 54 Ufset(); 55 memset(in,0,sizeof(in)); 56 if(u<=-1&&v<=-1)break; 57 if(u==0&&v==0){ 58 printf("Case %d is a tree.\n",cas++);continue; 59 } 60 fnode[1].u=u; 61 fnode[1].v=v; 62 in[v]++; 63 int ans=2; 64 int temp=0; 65 while(scanf("%d%d",&u,&v),u&&v) 66 { 67 fnode[ans].u=u; 68 fnode[ans++].v=v; 69 in[v]++; 70 if(in[v]>=2)temp=1; 71 } 72 if(temp){ 73 printf("Case %d is not a tree.\n",cas++);continue; 74 } 75 int i; 76 int flag=0; 77 for(i=1;i<ans;i++) 78 { 79 if(Find(fnode[i].u)!=Find(fnode[i].v)){ 80 Union(fnode[i].u,fnode[i].v); 81 } 82 else{ 83 flag=-1;break; 84 } 85 } 86 if(flag==-1) 87 { 88 printf("Case %d is not a tree.\n",cas++); 89 } 90 else 91 { 92 int i; 93 int tmp=0; 94 for(i=1;i<=100000;i++){ 95 if(parent[i]<-1) 96 tmp++; 97 } 98 if(tmp>=2){ 99 printf("Case %d is not a tree.\n",cas++); 100 } 101 else 102 printf("Case %d is a tree.\n",cas++); 103 } 104 } 105 return 0; 106 }