nyoj129 树的判定 (并查集)

 

树的判定

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
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.

 

 
输入

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.

The number of test cases will not more than 20,and the number of the node will not exceed 10000.
The inputs will be ended by a pair of -1.

输出

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).

样例输入
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
 
样例输出
Case 1 is a tree.
Case 2 is a tree.
Case 3 is not a tree.

题意:判断是否为树 有空树,但没有只有一个节点的树 1 1  0 0 不是树

算法分析:

法一:并查集判断如果父亲一样则 not a tree

法二:入度大于1 not a tree

 

法一:并查集poj 和nyoj 都过了 ;hdu1325  RE (应该是递归调用太多而栈溢出)

1 2 1 3 0 0

2 1 3 1 0 0

View Code
 1  #include<iostream>
 2  #include<string.h>
 3  #define N 10010
 4  using namespace std;
 5  
 6  int parent[N],visited[N],IN[N];
 7  
 8  int max(int a,int b)
 9  {
10      return a>b?a:b;
11  }
12  
13  int find(int x)
14  {
15      if(x==parent[x]) return x;
16      parent[x]=find(parent[x]);
17      return parent[x];
18  }
19  
20  int main()
21  {
22      int x,y,fx,fy,maxn,i,k=1;
23      bool flag=true;maxn=0;
24      memset(visited,false,sizeof(visited));
25      memset(IN,0,sizeof(IN));
26      for(i=0;i<N;i++) parent[i]=i;
27      while(cin>>x>>y)
28      {
29          if(x==-1&&y==-1) break;
30          if(x==0&&y==0)
31          {
32              int r=0;
33              for(i=1;i<=maxn;i++)
34              {
35                  if(visited[i]&&i==parent[i])
36                      r++;
37              }
38              if(r>1) flag=false;
39              r=0;
40              for(i=1;i<=maxn;i++)
41              {
42                  if(visited[i]&&IN[i]==0)
43                      r++;
44              }
45              if(r>1) flag=false;
46              if(flag) cout<<"Case "<<k++<<" is a tree."<<endl;
47              else cout<<"Case "<<k++<<" is not a tree."<<endl;
48              flag=true;maxn=0;
49              memset(visited,false,sizeof(visited));
50              memset(IN,0,sizeof(IN));
51              for(i=0;i<N;i++) parent[i]=i;
52          }
53          else
54          {
55              maxn=max(maxn,max(x,y));
56              visited[x]=visited[y]=true;
57              IN[y]++;
58              if(!flag) continue;
59              fx=find(x);
60              fy=find(y);
61              if(fx!=fy) parent[fy]=fx;
62              else flag=false;             
63          }
64      }
65      return 0;
66  }

 

 

法二:都能过 

View Code
 1 #include<iostream>
 2 #include<string.h>
 3 #define N 10010
 4 using namespace std;
 5 
 6 int parent[N],visited[N],IN[N];
 7 
 8 int max(int a,int b)
 9 {
10     return a>b?a:b;
11 }
12 
13 int find(int x)
14 {
15     if(x==parent[x]) return x;
16     parent[x]=find(parent[x]);
17     return parent[x];
18 }
19 
20 int main()
21 {
22     int x,y,fx,fy,maxn,i,k=1;
23     bool flag=true;maxn=0;
24     memset(visited,false,sizeof(visited));
25     memset(IN,0,sizeof(IN));
26     for(i=0;i<N;i++) parent[i]=i;
27     while(cin>>x>>y)
28     {
29         if(x==-1&&y==-1) break;
30         if(x==0&&y==0)
31         {
32             int r=0;
33             for(i=1;i<=maxn;i++)
34             {
35                 if(visited[i]&&i==parent[i])
36                     r++;
37             }
38             if(r>1) flag=false;
39             if(flag) cout<<"Case "<<k++<<" is a tree."<<endl;
40             else cout<<"Case "<<k++<<" is not a tree."<<endl;
41             flag=true;maxn=0;
42             memset(visited,false,sizeof(visited));
43             memset(IN,0,sizeof(IN));
44             for(i=0;i<N;i++) parent[i]=i;
45         }
46         else
47         {
48             maxn=max(maxn,max(x,y));
49             visited[x]=visited[y]=true;
50             IN[y]++;
51             if(IN[y]>1)flag=false;//判断入度大于1
52             if(!flag) continue; //防止栈溢出
53             fx=find(x);
54             fy=find(y);
55             if(fx!=fy) parent[fy]=fx;
56         }
57     }
58     return 0;
59 }

 

posted @ 2011-10-25 20:46  mtry  阅读(609)  评论(0编辑  收藏  举报