zoukankan      html  css  js  c++  java
  • hdu 1325 Is It A Tree? (树、node = edge + 1、入度 <= 1、空树)

    Is It A Tree?
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 29483    Accepted Submission(s): 6775

    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.

    C/C++:

     1 #include <map>
     2 #include <queue>
     3 #include <cmath>
     4 #include <vector>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cstring>
     8 #include <climits>
     9 #include <iostream>
    10 #include <algorithm>
    11 #define INF 0xffffff
    12 using namespace std;
    13 
    14 const int my_max = 100005;
    15 int a, b, my_node[my_max], my_in[my_max];
    16 
    17 int main()
    18 {
    19     int t = 1;
    20     while (scanf("%d%d", &a, &b), a >= 0 && b >= 0)
    21     {
    22         if (a == 0 && b == 0)
    23         {
    24             printf("Case %d is a tree.
    ", t ++);
    25             continue;
    26         }
    27 
    28         memset(my_node, 0, sizeof(my_node));
    29         memset(my_in, 0, sizeof(my_in));
    30 
    31         int my_edge = 1, my_flag = 0;
    32         my_node[a] = my_node[b] = my_in[b] = 1;
    33         while (scanf("%d%d", &a, &b), a || b)
    34         {
    35             if (my_in[b])
    36                 my_flag = 1;
    37             my_node[a] = my_node[b] = my_in[b] = 1;
    38             my_edge ++;
    39         }
    40 
    41         if (my_flag)
    42         {
    43             printf("Case %d is not a tree.
    ", t ++);
    44             continue;
    45         }
    46         int my_node_cnt = 0;
    47         for (int i = 1; i < my_max; ++ i)
    48         {
    49             if (my_node[i]) my_node_cnt ++;
    50         }
    51         if (my_node_cnt == my_edge + 1)
    52             printf("Case %d is a tree.
    ", t ++);
    53         else
    54             printf("Case %d is not a tree.
    ", t ++);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    忘记秘密利用python模拟登录暴力破解秘密
    ubuntu16.04 install qtcreator
    ubuntu16.04 pip install scrapy 报错处理
    Ubuntu18.04 和ubuntu16.04 apt源更新
    Ubuntu16.04主题美化
    ubuntu16.04上vue环境搭建
    基于fastadmin快速搭建后台管理
    python生成linux命令行工具
    nvidia驱动自动更新版本后问题解决 -- failed to initialize nvml: driver/library version mismatch
    学会使用Google搜索
  • 原文地址:https://www.cnblogs.com/GetcharZp/p/9454812.html
Copyright © 2011-2022 走看看