zoukankan      html  css  js  c++  java
  • poj1308 (Is It A Tree?) 并查集

    Is It A Tree?
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 39434   Accepted: 12999

    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.

    分析:这个题跟hdu1272(小希的迷宫)很像,不过这个题的边是有方向的,
    小希的迷宫没有方向,合并的时候只要岸边的指向合并即可。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int pre[200000],flag,N=0;
    int a[200000];
    void init()
    {
        for(int i=0;i<200000;i++)
        pre[i]=i;
    }
    int find(int x)
    {
        if(x!=pre[x])
            pre[x]=find(pre[x]);
        return pre[x];
    }
    void unit(int x,int y)
    {
        int rootx=find(x),rooty=find(y);
        if(rootx==rooty) flag=1;
        if(rootx!=rooty)
            pre[rootx]=pre[rooty];//x-->y 
    }
    
    int main()
    {
        int x,y,cas=0;
        N=flag=0;init();
        while(scanf("%d%d",&x,&y)!=EOF)
        {
            if(x==-1&&y==-1) break;
            if(!x&&!y)//一组数据 
            {
                if(flag) printf("Case %d is not a tree.
    ",++cas);
                else 
                {
                    int x=find(a[0]);//找出根 
                    for(int i=1;i<N;i++)
                    if(find(a[i])!=x) {flag=1;break;}
                    if(flag) printf("Case %d is not a tree.
    ",++cas);
                    else printf("Case %d is a tree.
    ",++cas);
                }
                init();
                N=flag=0;
            }
            else
            {
                unit(x,y);
                a[N++]=x;a[N++]=y;
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    script:查看历史sql执行信息
    爱在watir(1)----一切从搭讪开始
    最简单的视音频播放演示样例7:SDL2播放RGB/YUV
    Git flow的分支模型与及经常使用命令简单介绍
    操作系统开发之——打开A20
    阿牛的EOF牛肉串-记忆化搜索或动态规划
    LintCode-Copy Books
    Android:Unable to resolve target ‘android-8’问题解决
    编程规范(一 之kmalloc,fflush,fclose,char_init)
    androidannotations的background和UiThread配合使用參考
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9705597.html
Copyright © 2011-2022 走看看