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

    Is It A Tree?

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 353    Accepted Submission(s): 140
     
    Problem 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.
     
     
    Source
    North Central North America 1997
     
    Recommend
    Ignatius.L
     

    分析:判断是一棵树的条件:1、入度为0的一个,其他节点入度为1;2、不能存在环。输入时可判断是否存在成环或入度大于1的点,再判断入度为0点是否唯一即可。

    include<stdio.h>
    #include<string.h>
    int fa[1001];
    int rd[1001];
    int vis[1001];
    int  flag;
    
    int find(int x) {
        if (x != fa[x])
            fa[x] = find(fa[x]);
        return fa[x];
    }
    
    int main() {
        int i, a, b, f1, f2, T = 0, zero;
        while (1) {
            scanf("%d%d", &a, &b);
    
            if (a<0)
                break;
            printf("Case %d is ", ++T);
            if (a == 0 && b == 0) {
                printf("a tree.\n");
                continue;
            }
            memset(rd, 0, sizeof (rd));
            memset(vis,0,sizeof(vis));
            for (i = 1; i <= 1000; ++i)
                fa[i] = i;
            flag = 1;
            vis[a]=vis[b]=1;
            rd[b]++;
            f1 = find(a);
            f2 = find(b);
            if (f1 == f2 || b!=f2)
                flag = 0;
            else
                fa[f2] = f1;    
            while (1) {
                scanf("%d%d", &a, &b);
                if (a == 0 && b == 0)
                    break;         
                if (!flag)
                    continue;
                vis[a]=vis[b]=1;
                rd[b]++;
                f1 = find(a);
                f2 = find(b);
                if (f1 == f2 || f2!=b)
                    flag = 0;
                else
                    fa[f2] = f1;
            }
            if (flag) {
                zero=0;
                for(i=1;i<=1000;++i)
                {
                    if(vis[i] && rd[i]==0)
                    {
                        if(!zero)
                            zero=1;
                        else
                        {
                            flag=0;
                            break;
                        }
                    }
                }  
            }
            printf("%s\n", flag ? "a tree." : "not a tree.");
        }
        return 0;
    }
  • 相关阅读:
    SGU 205. Quantization Problem
    BZOJ1009: [HNOI2008]GT考试
    SGU 204. Little Jumper
    Spring Aspect 获取请求参数
    springboot 是用拦截器Interceptor获取请求的控制器和请求的方法名
    spring boot Filter过滤器的简单使用
    Java 使用Query动态拼接SQl
    java 传入list集合 返回树形菜单,for循环遍历
    spring data jpa 的简单使用
    vue项目引入element
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2672384.html
Copyright © 2011-2022 走看看