poj 1308 &&HDU 1325 Is It A Tree?(并查集、树的定义)

来源:互联网 发布:vs2015能开发php吗 编辑:程序博客网 时间:2024/05/22 15:53
Is It A Tree?
点击打开题目链接
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 21912 Accepted: 7483

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. 题目中给出的边是有向的
§3.树中不能存在环(这个容易想到)
§4.树中不存在自己指向自己的边
§5.树中的节点不能有两个父亲
§6.森林不是树(即只能有一个根节点)注:如在HDU上提交,请注意结束条件,不是a,b==-1,而是小于零结束;
§解题思路(并查集实现):
§判断有没有环:使用并查集判断两个要连接边的节点的祖先是否相同,相同则存在环;
§判断是否为森林:记录一下顶点数和边数(因为一棵树的边数总是比顶点数少一)。
§判断是否有两个父亲:判断一下是否要指向的节点的父节点是否为自己
§判断有无自己指向自己的边放到有无环的判断中;
代码:
#include <iostream>#include<stdio.h>#include<string.h>#define MAX 100010using namespace std;int f[MAX];int st[MAX],sp;void init(){    int i;    for(i=0; i<MAX; i++)    {        f[i]=i;    }    memset(st,0,sizeof(st));}int set_find(int x){    return f[x]==x?x:f[x]=set_find(f[x]);}int judge(int p){    int i;    for(i=0; i<sp; i++)    {        if(st[i]==p)            return false;    }    return true;}int main(){    int a,b,c,d,p1,p2,side;    bool flag;    int cnt=0;    while(~scanf("%d%d",&a,&b)&&!(a==-1&&b==-1))    {        init();        flag=false;        f[b]=a;        side=1;        if(a==b&&!(a==0&&b==0))            flag=true;        st[0]=a;        st[1]=b;        sp=2;        while((a||b)&&~scanf("%d%d",&c,&d)&&(c||d))        {            p1=set_find(c);            p2=set_find(d);            if(judge(c))                st[sp++]=c;            if(judge(d))                st[sp++]=d;            side++;            if(p2!=d)            {                flag=true;                continue;            }            if(p1==p2)                flag=true;            f[p2]=p1;        }        if((sp-1)!=side)            flag=true;        if(!flag)            printf("Case %d is a tree.\n",++cnt);        else            printf("Case %d is not a tree.\n",++cnt);    }    return 0;}



0 0
原创粉丝点击