hdu 1325 Is It A Tree? 并查集

Is It A Tree?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20222    Accepted Submission(s): 456


Problem 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.
 
Source
 

思路:给你一个树,判断是不是树;并查集只要判断是否只有一个集合;并没有说是从1开始

    坑点,这是有向的;

       1 2 3 2 0 0 //is not tree;

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define m 9973
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF ) return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
int father[100010];
int flag[100010];
int rudu[100010];
int findfa(int x)
{
    return x==father[x]?x:father[x]=findfa(father[x]);
}
void init()
{
    for(int i=1;i<=100000;i++)
    father[i]=i,flag[i]=0,rudu[i]=0;
}
int hebing(int u,int v)
{
    int xx=findfa(u);
    int yy=findfa(v);
    if(xx==yy)
    return 0;
    father[xx]=yy;
    return 1;
}
int check(int x)
{
    int i,t;
    for(i=1;i<=x;i++)
    if(flag[i])
    break;
    for(t=i;t<=x;t++)
    if(flag[t]&&findfa(i)!=findfa(t))
    return 0;
    return 1;
}
int check1(int x)
{
    int ji=0;
    int lu=0;
    for(int i=1;i<=x;i++)
    {
        if(flag[i])
        {
            if(rudu[i]==0)
            ji++;
            else if(rudu[i]>1)
            return 0;
        }
    }
    if(ji!=1)
    return 0;
    return 1;
}
int main()
{
    int x,z,i,t;
    int u,v;
    int casee=1;
    while(scanf("%d%d",&u,&v)!=EOF)
    {
        if(u<0||v<0)break;
        init();
        int flagg=0,maxx=0;
        while(1)
        {
            maxx=max(maxx,max(u,v));
            if(u==0&&v==0)break;
            flag[u]=1;
            flag[v]=1;
            rudu[v]++;
            if(!hebing(u,v))
            flagg=1;
            scanf("%d%d",&u,&v);
        }
        printf("Case %d ",casee++);
        if(flagg)
        printf("is not a tree.
");
        else if(check(maxx)&&check1(maxx))
        printf("is a tree.
");
        else  printf("is not a tree.
");
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jhz033/p/5421698.html