• 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
  • 相关阅读:
    多线程第四篇秒杀 一个经典的多线程同步问题
    用Maven整合SpringMVC+Spring+Hibernate 框架,实现简单的插入数据库数据功能
    用JSP实现的商城购物车模块
    深入浅出JMS(一)——JMS简单介绍
    hdu 1700 Points on Cycle 水几何
    回调函数
    VC中获取窗体句柄的各种方法
    virus.win32.parite.H病毒的查杀方法
    [WF4.0 实战] WPF + WCF + WF 打造Hello World(基础篇)
    HDU 4946 Area of Mushroom 凸包 第八次多校
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9705597.html
Copyright © 2020-2023  润新知