zoukankan      html  css  js  c++  java
  • 暑假集训(2)第一弹 -----Is It A Tree?(Poj308)

    A - Is It A Tree?

    Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu

    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,2)(2,1)或者(1,1)或者(1,5)(1,5)这些情况都是不行的。

    2.要点与点之间连通,而且注意空树(即无点无边的树)也是树。

     1 #include "cstdio"
     2 struct node
     3 {
     4     int i;
     5     int j;
     6 };
     7 node a[1000],b[1000];
     8 int number,time=0;
     9 int nbegin()
    10 {
    11    int i=0;
    12    time++;
    13    while (scanf ("%d%d",&a[i].i,&a[i].j)&& (a[i].i != -1 || a[i].j != -1))
    14    {
    15        number = i;
    16        if (a[i].i || a[i].j)
    17              i++;
    18        else
    19            return 1;
    20    }
    21    return 0;
    22 }
    23 void tree()
    24 {
    25    int now,next,q;
    26    node y;
    27    if (a[number].i == 0 && a[number].j == 0 && number == 0)
    28     {
    29             printf ("Case %d is a tree.
    ",time);
    30             return;
    31     }
    32     for (int i=0;i<number;i++)
    33     {
    34          for (int k=0;k<number;k++)
    35         {
    36          if (a[i].j == a[k].j && i != k)
    37             {
    38                 printf("Case %d is not a tree.
    ",time);
    39                 return;
    40             }
    41          if (a[i].i == a[k].j && a[i].j == a[k].i)
    42              {
    43                 printf("Case %d is not a tree.
    ",time);
    44                 return;
    45              }
    46         }
    47     }
    48    for (int i=0;i<number;i++)
    49    {
    50         now=0;
    51         next=0;
    52         q=0;
    53       for (int t=0;t<number;t++)
    54         if (a[t].i == a[i].i )
    55         {
    56           b[next++] = a[t];
    57           q++;
    58         }
    59       while (next != now)
    60             {
    61               y = b[now];
    62               now++;
    63               for (int j=0;j<number;j++)
    64                 if (y.j == a[j].i && y.i != a[j].j)
    65                 {
    66                         b[next++] = a[j];
    67                         q++;
    68                 }
    69             }
    70         if (q == number)
    71         {
    72                 printf ("Case %d is a tree.
    ",time);
    73                 return;
    74         }
    75     }
    76      printf("Case %d is not a tree.
    ",time);
    77 }
    78 int main()
    79 {
    80     while(nbegin())
    81         tree();
    82     return 0;
    83 }
    View Code












  • 相关阅读:
    Vue 项目运行 npm run dev 命令时会报错:“'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序” 的解决办法
    百度地图 操作步骤
    返回数据-----数组处理
    Echarts折线图,适配可移动
    jq checkbox的操作——全选、反选
    jquery 时间戳与日期转换
    Flex 布局
    iphone遇到INPUT标签BUG
    点击复制粘贴
    Python数据类型的可变与不可变
  • 原文地址:https://www.cnblogs.com/huas-zlw/p/5696945.html
Copyright © 2011-2022 走看看