zoukankan      html  css  js  c++  java
  • HDU--1325 并查集判树(有向无环图)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1325

    Is It A Tree?

    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.
        题意:判所给图是不是一棵树。
        解析:和HDU--1272基本一样。但是此图有方向。判定条件:1.不成环。  2.只有一个根,也就是有n-1条边,边数+1=点数。  3.节点入度为1,根的入度为0。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<set>
    using namespace std;
    const int maxn=1e5+10;
    int pr[maxn],v[maxn],c[maxn];
    int flag=0,sum=0;
    void init()
    {
        for(int i=1;i<maxn;i++)
        {
            pr[i]=i;
            v[i]=0;
            c[i]=0;
        }
    }
    int find(int x)
    {
        if(x!=pr[x])
            return pr[x]=find(pr[x]);
            return x;
    }
    void join(int a,int b)
    {
        int fa=find(a),fb=find(b);
        if(fa!=fb)
            pr[fa]=fb;
        else
            flag=1;
    }
    int main()
    {
        int a,b;
        int ac=1;
        while(scanf("%d%d",&a,&b))
        {
            flag=0;
            sum=0;
            init();        
            if(a<0&&b<0)
                break;
            if(a==0&&b==0)
            {
                printf("Case %d is a tree.
    ",ac);
                ac++;
                continue;
            }    
            v[a]=v[b]=1;
            join(a,b);   
            c[b]++; 
            sum++;
            while(scanf("%d%d",&a,&b))
            {
                if(a==0&&b==0)
                    break;
                join(a,b);
                v[a]=v[b]=1;
                c[b]++;
                sum++;
            }
            if(flag==1)
            {
                printf("Case %d is not a tree.
    ",ac++);
            }
            else
            {
                 int ok=0,cnt=0;
                 for(int i=1;i<maxn;i++)
                 {
                     if(v[i]==1)
                         cnt++;    
                    if(c[i]>1)
                        ok=1;
                }    
                if(cnt==(sum+1)&&ok==0)
                {
                    printf("Case %d is a tree.
    ",ac++);
                }
                else
                {
                    printf("Case %d is not a tree.
    ",ac++);
                }
            }
        }
    }
  • 相关阅读:
    每日二题20201117(34. 在排序数组中查找元素的第一个和最后一个位置)
    【CQOI2011】动态逆序对
    P3584 [POI2015]LAS
    P4643 [国家集训队]阿狸和桃子的游戏
    【CF555E】Case of Computer Network
    P3810 【模板】三维偏序(陌上花开)
    P3391 【模板】文艺平衡树(Splay)
    P3690 【模板】Link Cut Tree (动态树)
    【CF161D】Distance in Tree
    P3806 【模板】点分治1
  • 原文地址:https://www.cnblogs.com/liyexin/p/12655940.html
Copyright © 2011-2022 走看看