hdu 1325 Is It A Tree?

来源:互联网 发布:编程软件win10不支持 编辑:程序博客网 时间:2024/05/27 06:50
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.
与HDU 1272很相似,但是有所不同,前者是一个无向的树,而这一个是一个有向树,这两个的区别我们从一组样例看出
1 2 3 2 0 0
这一组样例如果是无向图就是YES,如果是有向图就是NO
那么怎么再无向图的基础上判断是不是有向图呢
其实只需要加上
if(x==y||b!=y)//b!=y         flag=0;
一个图想要连接另一个节点的话,只能连接另一个图的头节点,如果不是,flag就是0
下面代码
#include<set>#include<cstdio>#include<string.h>using namespace std;int pre[600086];bool vis[600086];int findboss(int x){    if(x==pre[x])        return x;    else    {        pre[x]=findboss(pre[x]);        return pre[x];    }}int main(){    int a,b;    int kase=1;    while(scanf("%d%d",&a,&b)!=EOF)    {        int flag=1;        for(int i=0; i<600086; i++)            pre[i]=i,vis[i]=0;        if(a==-1&&b==-1)            break;        if(a==0&&b==0)        {            printf("Case %d is a tree.\n",kase++);            continue;        }        vis[a]=vis[b]=1;        int x=findboss(a);//第一个指向第二个        int y=findboss(b);        if(x==y||b!=y)            flag=0;        else        {            pre[y]=x;        }        while(1)        {            scanf("%d%d",&a,&b);            if(a+b==0)                break;            vis[a]=vis[b]=1;            int x=findboss(a);            int y=findboss(b);            if(x==y||b!=y)                flag=0;            else            {                pre[y]=x;            }        }        int time=0;        for(int i=0; i<600086; i++)        {            if(vis[i]&&pre[i]==i)                time++;        }        // printf("%d %d",c,ycq.size()-1);        if(flag&&time==1)            printf("Case %d is a tree.\n",kase++);        else           printf("Case %d is not a tree.\n",kase++);    }}

原创粉丝点击