zoukankan      html  css  js  c++  java
  • 5.1.2 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): 480 Accepted Submission(s): 174

    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.

    思路:利用树的定义!节点数=边数+1!!

    对于重边,可以用bool数组判断!但是题目上好像没说数据范围!

    57 cssystem 0MS 360K 938B G++ 2013-02-08 11:51:13
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <string>
     7 #include <set>
     8 using namespace std;
     9 
    10 const int maxn=10010;
    11 set<int> s;
    12 int x,y,node,bian,cnt;
    13 bool f[maxn],flag;
    14 
    15 void close()
    16 {
    17     exit(0);
    18 }
    19 
    20 void work()
    21 {
    22 }
    23 
    24 void init ()
    25 {
    26 bian=0;cnt=0;flag=false;
    27     while (scanf("%d %d",&x,&y)!=EOF)
    28      {
    29          if (x==-1 && y==-1) break;
    30          if (x==0 && y==0) // print cnt
    31          {
    32              cnt++;
    33              node=s.size();
    34             // printf("Case:%d node:%d bian+1:%d \n",cnt,node,bian+1);
    35              if (node==bian+1 && not flag)
    36                  printf("Case %d is a tree.\n",cnt);
    37              else printf("Case %d is not a tree.\n",cnt);
    38              flag=false;
    39              s.clear();
    40              memset(f,false,sizeof(f));
    41              bian=0;
    42              continue;
    43          }
    44          if (f[y])
    45             flag=true;
    46          f[y]=true;
    47          s.insert(x);
    48          s.insert(y);
    49          bian++;
    50      } 
    51 }
    52 
    53 int main ()
    54 {
    55     init();
    56     work();
    57     close();
    58     return 0;
    59 }
  • 相关阅读:
    在X++中使用IoC/DI模式应对不断变化的客户需求
    Predicate<T>与Func<T, bool>泛型委托
    Windows Live Writer插件:在WLW中插入语法高亮代码
    学习C#和.NET的资源
    C#中事件的动态调用
    2008年全国软件工程大会论文集
    C#基础:接口(二)
    【转载】"变化"、"复用"、"抽象"、"稳定" 影响着软件设计模式,架构,开发方法
    【领域驱动设计】.NET实践:实体、值对象和数据传输对象
    RSS订阅之基本使用
  • 原文地址:https://www.cnblogs.com/cssystem/p/2909217.html
Copyright © 2011-2022 走看看