zoukankan      html  css  js  c++  java
  • Is It A Tree?

    Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

    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.

    //题目大意:判断是否为树,输入0 0结束循环,-1 -1结束程序,6 8代表6为8的根结点。树的定义可以百度。
    注意的地方如下:
          1: 0 0  空树是一棵树 
          2: 1 1 0 0  不是树 不能自己指向自己
          2: 1 2 1 2 0 0  不是树 重复都不行= =
    2: 1 2 2 1 0 0 也是错误 
          3: 1 2 2 3 4 5  不是树,森林不是树

    代码如下:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    const int maxn=105;
    int a,b,fir,dad[maxn];
    int f[maxn];
    
    void fuqin()
    {for (int x=1;x<=105;x++)
       {dad[x]=x;
       f[x]=0;}
    }
    
    int find(int x) {
        int p = x, t;
        while (dad[p] != p) p = dad[p];
        while (x != p) { t = dad[x]; dad[x] = p; x = t; }
        return x;
    }
    void union1(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return;
        dad[y] = x;
    }
    
    int main()
    {
            int t=1;
    while (scanf("%d %d",&a,&b)!=EOF)
    {       if(a==-1&&b==-1)break;
            if(a==0&&b==0)
            {printf("Case %d is a tree.
    ", t ++);continue;}//  第1类判断: 空树是一棵树。
            fuqin();
            f[a]=f[b]=1;
            fir = a;
            bool tree = 1;
            if(a == b)
                tree = 0;
            else
                union1(a, b);
            while(scanf("%d %d", &a, &b) && a != 0){
                f[a] = f[b] = 1;
                if(find(a) == find(b))
                    tree = 0;//  第2类判断。
                union1(a,b);
            }
            for(a = 1; a < 100; a ++)//  第3类判断:不能为森林。
                if(f[a] && find(a) != find(fir))
                    tree = 0;
            if(tree) printf("Case %d is a tree.
    ", t ++);
            else printf("Case %d is not a tree.
    ", t ++);
        }
        return 0;
    }
    View Code



       
  • 相关阅读:
    i++循环与i–循环的执行效率
    嵌入式linux通用截图工具
    imgsed发布
    Embedded Linux From Scratch
    WordPress Plupload插件未明跨站脚本漏洞
    Microsoft Internet Explorer 信息泄露漏洞
    WordPress 多个安全漏洞
    dedecms plus/search.php 注入漏洞利用EXP
    JBoss Enterprise Portal Platform多个跨站脚本执行漏洞
    WordPress organizer/page/users.php脚本多个跨站脚本漏洞
  • 原文地址:https://www.cnblogs.com/gdvxfgv/p/5697154.html
Copyright © 2011-2022 走看看