| Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
Description
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
Output
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.
#include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; #define maxn 105 bool flag[maxn]; int set[maxn]; void make_set() { for(int i=1;i<=100;i++) { flag[i]=false;set[i]=i; } } int find_set(int x) { while(x!=set[x]) x=set[x]; return x; } void union_set(int x,int y) { x=find_set(x); y=find_set(y); if(x==y)return; set[y]=x; } int main() { int i,x,y,V=1; while(scanf("%d%d",&x,&y)!=-1) { if(x==-1&&y==-1)break; if(x==0&&y==0) { printf("Case %d is a tree.\n",V++); continue; } make_set(); int root=x; bool tree=true; flag[x]=flag[y]=true; if(x==y)tree=false; else union_set(x,y); while(scanf("%d%d",&x,&y)) { if(x==0&&y==0)break; flag[x]=flag[y]=true; if(find_set(x)==find_set(y)) tree=false; union_set(x,y); } for(i=1;i<=100;i++) if(flag[i]&&find_set(i)!=find_set(root)) tree=false; if(tree) printf("Case %d is a tree.\n",V++); else printf("Case %d is not a tree.\n",V++); } return 0; }
6339



被折叠的 条评论
为什么被折叠?



