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;
    }
  • 相关阅读:
    【MongoDb入门】15分钟让你敢说自己会用MongoDB了
    【干货】基于Owin WebApi 使用OAuth2进行客户端授权服务
    【学习】在Windows10平台使用Docker ToolBox安装docker(一)
    快速搭建WebAPI(Odata+Code-First)附Odata条件查询表~
    使用QuertZ组件来搞项目工作流(一)
    AspNetCore 使用NLog日志,NLog是基于.NET平台开的类库!(又一神器)
    AspNetCore 基于流下载文件与示例代码
    再见了Server对象,拥抱IHostingEnvironment服务对象(.net core)
    AspNetCore 文件上传(模型绑定、Ajax) 两种方式 get到了吗?
    AspNetCore 目前不支持SMTP协议(基于开源组件开发邮件发送,它们分别是MailKit 和 FluentEmail )
  • 原文地址:https://www.cnblogs.com/rain-1/p/4878364.html
Copyright © 2011-2022 走看看