• PAT 1004 Counting Leaves


    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

    Input Specification:

    Each input file contains one test case. Each case starts with a line containing 0<N<100, the number of nodes in a tree, and M (<N), the number of non-leaf nodes. Then M lines follow, each in the format:

    ID K ID[1] ID[2] ... ID[K]
    

    where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.

    The input ends with N being 0. That case must NOT be processed.

    Output Specification:

    For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.

    The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

    Sample Input:

    2 1
    01 1 02
    

    Sample Output:

    0 1


    题意:输入n个节点,m个非叶子结点
    m行,节点id,子节点个数k,k个节点id
    思路:
    定义一个结构体保存其父节点,是否是叶子结点,所在的层数;
    感觉这样比dfs简单一点。
     1 #include<iostream>
     2 using namespace std;
     3 
     4 struct Node
     5 {
     6     int father;
     7     int level;
     8     bool leaf;
     9 };
    10 Node no[105];
    11 int level[105];//记录每层有多少叶子结点
    12 int main()
    13 {
    14     int n,m,id,k,ca,maxle = 1;//如果maxle = -1,测试点2会出错
    15     cin >> n >> m;
    16     //初始化
    17     for(int i = 0;i <= n;i++)
    18     {
    19         no[i].father = 0;
    20         no[i].level = 0;
    21         no[i].leaf = 1;
    22     }
    23     no[1].level = 1;//第一层
    24     for(int i = 0;i < m;i++)
    25     {
    26         cin >> id >> k;
    27         no[id].leaf = 0;
    28         for(int j = 0;j < k;j++)
    29         {
    30             cin >> ca;
    31             no[ca].father = id;
    32         }
    33     }
    34 
    35     for(int i = 1;i <= n;i++)
    36     {
    37         for(int j = 1;j <= n;j++)
    38         {
    39             if(no[j].father == i)//父节点
    40             {
    41                 no[j].level = no[i].level + 1;//层数为父节点的下一层
    42                 if(no[j].level > maxle)
    43                     maxle = no[j].level;//更新最大层数
    44             }
    45         }
    46         if(no[i].leaf == 1)
    47             level[no[i].level]++;//更新每层的叶子结点
    48     }
    49     for(int i = 1;i < maxle;i++)
    50         cout << level[i] << " ";
    51     cout << level[maxle] << endl;
    52     return 0;
    53 }

    我一直错一个测试点2,因为把最大层数的初始值定义成了-1,改成1就对了。。。。。








  • 相关阅读:
    Arthas线上问题排查
    如何快速增加pdf书签,解除pdf限制
    数组指针——指向数组的指针(通过指针控制数组)
    指针和数组直接对应关系之如何记忆
    C/C++语言中指针数组和数组指针比较区别
    int最大值+1为什么是-2147483648最小值-1为什么是2147483647
    电脑进行二进制加减运算方法
    C/C++语言中的函数参数传参三种对比
    Python基于VS2013 开发环境搭建 Hello World 10分钟搞定
    算法的复杂度包括时间复杂度和空间复杂度分别如何计算?
  • 原文地址:https://www.cnblogs.com/lu1nacy/p/10046711.html
Copyright © 2020-2023  润新知