并查集之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;
}


 

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