zoukankan      html  css  js  c++  java
  • 判断是否是树(Is It A Tree?)

    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.
     
     
     
    问题描述:
    输入多组测试案例,每组测试案例以-1,-1结束
    每组案例中输入多组数据n,和m, 其中n指向m, 输入以0,0结束
    判断上述输入得到的图是否是树
     
    解题思路:
       满足树的条件:
            1:只能有一个根节点
            2:不能有环
            3:根节点入度为0, 其余节点入度为1(判断的时候直接判断是否所有节点的入度都小于等于1即可)
     
      一直wrong answer的原因:
                  1:刚开始一直忘记删除重定向语句,我哩个去,一直纠结,够了,够了,这都发现不了
                  2:判断树的时候只用了两个条件 
                     a:只能有一个树根
                     b:不能有环
                   我原来以为满足这两个条件,所有节点的入度就都满足条件了,结果发现也可能存在节点入度大于1的情况
    #include <stdio.h>
    #define max_num  100000+10
    typedef struct
    {
        int vis, root, conn;
    }Node;
    Node node[max_num];
    void init()
    {
        for(int i = 0; i < max_num; i++)
        {
            node[i].vis = 0;
            node[i].root = i;
            node[i].conn = 0;
        }
    }
    int find_root(int x)
    {
        while(x != node[x].root)
            x = node[x].root;
        return x;
    }
    void union_set(int x, int y)
    {
        x = find_root(x);
        y = find_root(y);
        if(x != y)
            node[y].root = x;
    }
    int main()
    {
        int n, m;
        bool flag = true;
        int case_num = 1;
        init();
        //freopen("input.txt", "r", stdin);
        while(scanf("%d%d", &n, &m), n >= 0 && m >= 0)
        {
            if(!flag && (n != 0 && m != 0))
                continue;
            if(n == 0 && m == 0)
            {
                int root_num = 0;
                for(int i = 1; i < max_num; i++)
                {
                    if(find_root(i) == i && node[i].vis)
                        root_num++;
                    if(node[i].conn > 1)
                        flag = false;
                }
                if(root_num > 1)
                    flag = false;
                if(flag)
                    printf("Case %d is a tree.
    ", case_num++);
                else
                    printf("Case %d is not a tree.
    ", case_num++);
                flag = true;
                init();
                continue;
            }
            if(n != m && find_root(n) == find_root(m))
                flag = false;
            else
            {
                node[n].vis = 1;
                node[m].vis = 1;
                node[m].conn++;
                union_set(n, m);
            }
        }
        return 0;
    }
  • 相关阅读:
    PHP处理字符中的emoji表情
    CentOS 7 安装 PHP7.2 (LNMP环境搭建第二步)
    php 二维数组按某个字段排序
    JavaScript返回到上一页的方法
    常用正则表达式—手机号码
    JS实现斐波那契列数的三种方法
    常用正则表达式--金额
    PHP中三种设置脚本最大执行时间的方法
    生成指定长度随机字符串
    API常用签名验证方法(PHP实现)
  • 原文地址:https://www.cnblogs.com/rain-1/p/4878364.html
Copyright © 2011-2022 走看看