zoukankan      html  css  js  c++  java
  • POJ1308--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): 17512    Accepted Submission(s): 3936


    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
     

     

    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  1856 1102 1875 1879 1301 
    PojAC代码:
     
     1 #include <stdio.h>
     2 #include <string.h>
     3 int set[10010], father[10010];
     4 int find(int a) 
     5 {
     6     while(a != father[a])
     7     a = father[a];
     8     return a;
     9 }
    10 int mercy(int a, int b)
    11 {
    12     int q = find(a);
    13     int p = find(b);
    14     if(q != p)
    15     {
    16         father[q] = p;
    17         return 1;
    18     }
    19     return 0; 
    20 }
    21 int main()
    22 {
    23     int i, m, n, ac = 1;
    24     while(~scanf("%d %d", &m, &n))
    25     {
    26         if(m<0 && n<0)
    27         break;
    28         if(m==0 && n==0)            //
    29         {
    30             printf("Case %d is a tree.
    ", ac++);
    31             continue;
    32         }
    33         for(i=0; i<10010; i++)
    34         {
    35             father[i] = i;
    36             set[i] = 0;
    37          }
    38          int flag = 1;
    39          if(find(m) == find(n))       //
    40          flag = 0;
    41          mercy(m, n);
    42          set[m] = set[n] = 1;
    43          while(~scanf("%d %d",&m, &n), m+n)
    44          {
    45              if(find(m) == find(n))
    46             flag = 0;
    47             mercy(m, n);
    48             set[m] = set[n] = 1;
    49         }
    50         if(flag == 0)
    51         {
    52         
    53             printf("Case %d is not a tree.
    ", ac++);
    54             continue;
    55         }
    56         int total = 0;
    57         for(i=0; i<10010; i++)
    58         {
    59             if(father[i] == i && set[i])
    60             total++;
    61             if(total>1)
    62             break;
    63         }
    64         if(total>1)
    65         printf("Case %d is not a tree.
    ", ac++);
    66         else 
    67         printf("Case %d is a tree.
    ", ac++);
    68     }
    69     return 0;
    70 }

    //毁一生啊, 在HDoj浪费多少时间%>_<%。

  • 相关阅读:
    169. Majority Element求众数
    567. Permutation in String字符串的排列(效率待提高)
    51. N-Queens N皇后
    52. N-Queens II N皇后II
    layui的form.val无法动态渲染赋值表单问题解决方法
    layui 父页面获取弹窗传递的值 和 父页面传值给子弹窗的方法
    layui搜索框,监听为空,表格重新加载
    layui form表单 input输入框获取焦点后 阻止Enter回车自动提交
    5月17日 日期格式遇到一些问题
    Layui关闭弹出层并刷新父窗口
  • 原文地址:https://www.cnblogs.com/soTired/p/4680213.html
Copyright © 2011-2022 走看看