Poj1308 Is It A Tree? 并查集

来源:互联网 发布:java读取usb扫描枪 编辑:程序博客网 时间:2024/06/05 11:42

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.

Source

North Central North America 1997

题意是让你判断一些点是否构成了一棵树,有几种需要注意的情况:

1、空树也是树;

2、树不可能成环;

3、每个节点最多只有一个父亲节点;

4、不能是森林。

该问题可以用并查集解决。

对于1,特判即可。

对于2,判断所所插入的前一个节点的父亲节点是否为后一个节点,是的话,则成环。

对于3,用一个 v 数组记录节点是否有父亲节点。

对于4,设一个 s 数组,记录所有已访问过的节点,并求出这些节点中,其父亲节点为本身的结点个数,若大于一,则是森林。



#include <iostream>#include <cstdio>#include <set>#include <vector>#include <queue>#include <stack>#include <cmath>#include <algorithm>#include <cstring>#include <string>using namespace std;#define INF 0x3f3f3f3ftypedef long long LL;bool v[3001],s[3001];int num[10010];int p[3001];int gp(int x){    if(p[x]!=x){        p[x]=gp(p[x]);    }    return p[x];}void merrg(int x,int y){    int p1=gp(x);    int p2=gp(y);    if(p1==p2){        return;    }    p[p2]=p1;}int main(){    int x,y;    int ca=0;    while(~scanf("%d%d",&x,&y)){        ca++;        int cnt=0;        int flag=0;        if(x==-1&&y==-1){            break;        }        if(x==0&&y==0){           printf("Case %d is a tree.\n",ca);           continue;        }        for(int i=0;i<3001;i++){            p[i]=i;        }        memset(s,false,sizeof(s));        memset(v,false,sizeof(v));        if(x!=0&&y!=0){            memset(num,0,sizeof(num));            if(x==y&&x!=0){                flag=1;            }            v[y]=true;            s[x]=s[y]=true;            merrg(x,y);            while(~scanf("%d%d",&x,&y)){                if(x==0&&y==0){                    break;                }                if((x==y&&x!=0)||v[y]||gp(x)==y){                    flag=1;                }                merrg(x,y);                v[y]=true;                s[x]=s[y]=true;            }            int sum=0;            for(int i=1;i<=3001;i++){                if(s[i]&&gp(i)==i){                    sum++;                }            }            if(sum>1){                flag=1;            }            if(flag==1){                printf("Case %d is not a tree.\n",ca);            }else{                printf("Case %d is a tree.\n",ca);            }        }    }    return 0;}


0 0
原创粉丝点击