HDU 1325 Is It a Tree?

来源:互联网 发布:打印工厂软件下载 编辑:程序博客网 时间:2024/05/21 06:51
 

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4099    Accepted Submission(s): 962


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 45 6 0 08 1 7 3 6 2 8 9 7 57 4 7 8 7 6 0 03 8 6 8 6 45 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)、判断节点的入度是否<=1,如果大于1,则说明不是树;
(3)、判断顶点数是否等于边数加1,如果不等,则说明不是树。
 
 
 
代码如下:
 

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;

int father[100005];int ingree[100005];int flag[100005];

int find_father(int x){   int r=x;   while(r!=father[r])      r=father[r];   while(x!=r)   {      int temp=father[x];      father[x]=r;      x=temp;   }   return r;}

void unit(int a,int b){   int r1=find_father(a),r2=find_father(b);   if(r1!=r2)      father[r1]=r2;}

int main(){   int a,b;   int cnt=0;   while(1)   {      scanf("%d%d",&a,&b);      for(int i=1;i<=100000;i++)      {         father[i]=i;         ingree[i]=0;         flag[i]=0;      }      bool p=0;      int edge=0;      cnt++;      if(a<0&&b<0) break;      if(!a&&!b)      {         printf("Case %d is a tree.\n",cnt);         continue;      }      if(find_father(a)!=find_father(b))         unit(a,b);      else p=1;      ingree[b]++;      flag[a]=flag[b]=1;      int min=a>b?b:a;      int max=a>b?a:b;      edge++;      while(scanf("%d%d",&a,&b),a||b)      {         if(find_father(a)!=find_father(b))            unit(a,b);         else p=1;         ingree[b]++;         if(ingree[b]>1) p=1;         flag[a]=flag[b]=1;         if(min>a) min=a;         if(min>b) min=b;         if(max<a) max=a;         if(max<b) max=b;         edge++;      }      if(!p)      {         int k=0;         for(int i=min;i<=max;i++)            if(flag[i])               k++;         if(k!=edge+1) p=1;      }      if(p)         printf("Case %d is not a tree.\n",cnt);      else          printf("Case %d is a tree.\n",cnt);   }      return 0;}