並查集之poj 1308

 

Is It A Tree?Crawling in process... Crawling failed

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u  

    

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.

 

題意:每組數據以一對0 0結束,整個程序以一對-1 -1結束,每一對數字x y代表y是x的子結點,即有一條有向邊由x指向y,問題是,給出的數據是否構成一顆樹。
題解:首先要知道如何才能構成一棵樹,以下有幾種情況不符合的:
1.根結點有多個,則有多棵樹。

2、每一個兒子只有一個父親;

3、不能自己是自己的父親;

4、兩父子不能重複

5、也是最容易被坑的地方:不能成圈而且空樹也是一顆樹的(在這兒被坑了一下午。。。。)

 

代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int countn,flag,flag1,z[1005],menber,r,father;

struct Node{
    int x;
    int num;//用來記錄當了多少次孩子節點
}No[1005];

int finda(int a)//查找根節點並壓縮路徑
{
    int i,j;r=a;
    while(No[r].x!=r)
    {
        r=No[r].x;
    }
    i=a;
    while(i!=r)
    {
        j=No[i].x;
        No[i].x=r;
        i=j;
    }
    return r;
}
void init(int a,int b)
{
    int f1,f2;
    if(a==b) flag=1;//自己是自己父親的情況
    f1=finda(a);f2=finda(b);
    if(f1!=f2)
    {
        No[f2].x=f1;
        countn++;
    }
    No[b].num++;
    if(No[b].num>1)
        flag=1;//當了孩子節點的次數大於1直接排除
}
int main()
{
    int a,b,i,y=1;
    while(scanf("%d %d",&a,&b))
    {
        flag=0;flag1=1;menber=0;countn=0;
        memset(z,0,sizeof z);
        for(i=0;i<1005;i++)
        {
            No[i].x=i;
            No[i].num=0;
        }
        if(a==-1&&b==-1)
            break;
        else if(a==0&&b==0)
        {
            printf("Case %d is a tree.\n",y++);
            flag1=0;
        }
        else {
                init(a,b);
                z[a]++;z[b]++;
            while(scanf("%d %d",&a,&b)&&(a||b))
            {
                init(a,b);z[a]++;z[b]++;
            }
        }
        for(i=0;i<1005;i++)
        {
            if(z[i])
              menber++;
        }
        if(flag1)
        {
            father=No[r].x;//判斷是否成圈
            if(flag==0&&(menber-countn==1)&&(No[father].num==0))
            printf("Case %d is a tree.\n",y++);
        else
            printf("Case %d is not a tree.\n",y++);
        }

    }
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章