POJ1308 Is It A Tree?

来源:互联网 发布:淘宝卖家发快递价格 编辑:程序博客网 时间:2024/05/16 03:08

题意:

判断是否是一棵树。树中每个节点的入度不能超过1,另外节点数等于边数-1。用find()查找两个节点a,b的祖先是否相同,不同就将f[b]=a,将前一个节点设为他的上一个层的相连的节点,用num[]数组记录后一个节点的入度。

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.
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int f[100005];int vis[100005];int num[100005];int find(int x){    return x==f[x]?x:find(f[x]);}int main(){    int u,v;    int a,b;    int cnt,flag,ans;    int time=1;    while(cin>>a>>b)    {        for(int i=0;i<=100005;i++)            f[i]=i;        cnt=0;        flag=0;        ans=0;        if(a==0&&b==0)        {            cout<<"Case "<<time++<<" ";            cout<<"is a tree."<<endl;            continue;        }        memset(vis,0,sizeof(vis));        memset(num,0,sizeof(num));        if(a==-1&&b==-1)            break;        int x=find(a);        int y=find(b);        if(!vis[a])        {            vis[a]=1;            cnt++;        }        if(!vis[b])        {            vis[b]=1;            cnt++;        }        num[b]++;        if(num[b]>1)            flag=1;        if(x!=y)        {            f[y]=x;            ans++;        }        else            flag=1;        while(cin>>u>>v,u+v)        {            x=find(u);            y=find(v);            if(!vis[u])            {                vis[u]=1;                cnt++;            }            if(!vis[v])            {                vis[v]=1;                cnt++;            }            num[v]++;            if(num[v]>1)                flag=1;            if(x!=y)            {                f[y]=x;                ans++;            }            else                flag=1;        }        cout<<"Case "<<time++<<" ";        if(!flag&&ans==cnt-1)            cout<<"is a tree."<<endl;        else            cout<<"is not a tree."<<endl;    }    return 0;}