Hdu1325Is It A Tree?


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.

Hdu1325Is It A Tree?_第1张图片 Hdu1325Is It A Tree?_第2张图片 Hdu1325Is It A Tree?_第3张图片

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,树根的入度是0、另外、树不能成环,保证每一个子节点的父亲只有一个,如果有两个还是不能叫树、了解到这些个内容,我们就可以分块来讨论问题了、

首先是不能自环,自环的条件是什么捏?当我们在找a,b(某两个要连接的点)的祖先的时候,如果他们的祖先相同,说明他们在同一个集合中,如果他们在同一个集合中,并且连接了,那么他们就自环了。

题解:

#include <cstdio> #include <iostream> #include <cstring> #include <string> #include <cmath> #include <queue> #include <set> #include <vector> #include <stack> #include <algorithm> #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) #define mem(a) memset(a, 0, sizeof(a)) #define eps 1e-5 #define M 100005 #define INF 0x3f3f3f3f using namespace std; int pre[100005]; int flag; int vis[M]; int find(int x) {     int r=x;     while(r!=pre[r])     {         r=pre[r];     }     int w=x;     while(w!=r)     {         int u=pre[w];         pre[w]=r;         w=u;     }     return r; } void mx(int a,int b) {     int x=find(a);     int y=find(b);     if(x!=y)     {         pre[x]=y; } else {     flag=0;//不能有同一个根节点 } } int main() {     int a,b;       int k=1;     while(cin>>a>>b)     {          flag=1;         if(a==0&&b==0)//注意当a,b为0时输出它是一个树。         {             printf("Case %d is a tree.\n",k++);             continue;         }         if(a<0&&b<0)         {             break;         }         for(int i=1;i<100003;i++)         {             pre[i]=i;             vis[i]=0;         }         mx(a,b);         vis[a]=1;         vis[b]=1;         while(cin>>a>>b)         {             if(a==0&&b==0)                 break;                 vis[a]=1;                 vis[b]=1;                 int x1=find(a);                 int x2=find(b);                 if(x1==x2||x2!=b)                     flag=0;             mx(a,b);         }         int sum=0;         if(!flag)         {          printf("Case %d is not a tree.\n",k++);         }         else         {             for(int i=1;i<100003;i++)             {                 if(vis[i]&&pre[i]==i)                 {                     sum++;                 }             }             if(sum==1)             {             printf("Case %d is a tree.\n",k++);             }             else             {               printf("Case %d is not a tree.\n",k++);             }         }     } }

你可能感兴趣的:(Hdu1325Is It A Tree?)