zoukankan      html  css  js  c++  java
  • HDU 1325 Is It A Tree? 判断是否为一棵树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1325

    POJ同题:http://poj.org/problem?id=1308

    Is It A Tree?

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 12963    Accepted Submission(s): 2919


    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。
    注意,有环的图的情况:要么不存在入度为0的结点,要么存在入度大于1的点。
    这样根据以上性质进行编程,如下:
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 const int maxn = 10010;
     7 bool vis[maxn];
     8 int indeg[maxn];
     9 int main()
    10 {
    11     int cas = 0;
    12     int a, b;
    13     while(scanf("%d%d", &a, &b),a>=0||b>=0)
    14     {
    15         if(a==0 && b==0)
    16         {
    17             printf("Case %d is a tree.
    ", ++cas);
    18             continue;
    19         }
    20         int maxx = 0;
    21         if(a > maxx) maxx = a;
    22         if(b > maxx) maxx = b;
    23         memset(vis, false, sizeof(vis));    //该结点是否存在
    24         memset(indeg, 0, sizeof(indeg));
    25         vis[a] = vis[b] = true;     //设置结点存在
    26         indeg[b]++;                 //入度+1,注意是有顺序的
    27         while(scanf("%d%d", &a, &b), a!=0||b!=0)
    28         {
    29             if(a > maxx) maxx = a;
    30             if(b > maxx) maxx = b;
    31             indeg[b]++;             //入度+1
    32             vis[a] = vis[b] = true; //设置结点存在
    33         }
    34         bool flag = true;
    35         int k = 0;
    36         for(int i = 1; i <= maxx; i++)
    37             if(vis[i] && indeg[i] > 1)    //入度大于1,说明一个结点有两个父结点,违背树的性质
    38             {
    39                 flag = false;
    40                 break;
    41             }
    42         for(int i = 1; i <= maxx; i++)
    43             if(vis[i] && indeg[i] == 0)     //入度等于0的点有两个以上(包括)的话,则说明根结点不唯一,可能是森林
    44                 k++;
    45         if(!flag || k != 1)
    46             printf("Case %d is not a tree.
    ", ++cas);
    47         else
    48             printf("Case %d is a tree.
    ", ++cas);
    49     }
    50     return 0;
    51 }
    View Code
  • 相关阅读:
    PyCharm 2018 最新激活方式总结(最新最全最有效)!!!
    Python入门编程中的变量、字符串以及数据类型
    用列主元消去法分别解方程组Ax=b,用MATLAB程序实现(最有效版)
    ISE 14.7安装教程最新版(Win10安装)——解决Win10安装完后打不开快捷方式的方法
    Python零基础入门必知
    Python安装教程最新版
    MATLAB实现连续周期信号的频谱分析(正余弦波信号举例)
    Android 基于帧布局实现一个进度条 FrameLayout+ProgressBar
    Android 使用RadioGroup和RadioButton实现单选效果
    Android 使用CheckBox实现多选效果
  • 原文地址:https://www.cnblogs.com/dzkang2011/p/tree_1.html
Copyright © 2011-2022 走看看