zoukankan      html  css  js  c++  java
  • 1004 Counting Leaves (30)(30 分)

    1004 Counting Leaves (30)(30 分)

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

    Input

    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.

    Output

    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 0 leaf 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


    题目翻译:

    1004.计算叶子个数

    一个家庭的层级结构经常被表现为一个家谱树。你的任务是统计这些家庭成员中谁没有孩子。

    输入

    每个输入文件包含一个测试实例。每个实例开始的一行包含N和M,N指树中的结点个数(0<N<100),M指非叶结点的个数。然后下面有M行,每行的格式如下:

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

    ID是一个两位数的数字,表示一个非叶结点。K表示其孩子的数量。随后是一个序列,序列中是该结点的孩子结点的两位数ID。为了简单起见,我们把根结点的ID固定为01。

    输出

    对于每个测试实例,你应该计算从根结点开始的每一层中没有孩子的家庭成员的个数。数字必须在一行内输出,用空格分隔,在每行结尾不能有多余的空格。

    测试样例表示了一个只有两个结点的树,01是根结点,02是它仅有的孩子。因此在根结点01层级,没有叶节点。再下一层级,有一个叶结点。然后我们应该在一行内输出“0 1”。

    // 从根节点 1 开始 逐层建树, 第一层 、 第二层 , 防止出现 第 i+1 层 使用 第 i 层 父亲的信息时, 父亲的信息没有初始化
    
    
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    
    #define maxn 200
    #define LL long long
    
    struct node {
        // 父亲的下标 
        int father;
        // 节点当前 所在的层次
        int level;
        // 节点是否有子节点 , 没有子节点就是需要记录统计的节点 
        bool noKids;
    };
    
    int N, M, ID, K, son;
    node Node[maxn];
    
    int level[maxn];
    int MaxLevel;
    
    void init() {
    
        for (int i = 0; i <= N; i++) {
            Node[i].father = 0;
            Node[i].level = 0;
            Node[i].noKids = true;
        }
    
        memset(level, 0, sizeof(level));
        MaxLevel = 1; // 当只有一个根节点时 , 最大层次 是 1 (在这里 Wa 了一个 样例)
    }
    
    int main() {
    
        while (cin >> N >> M) {
    
            init(); // 初始化信息
    
            // M 个非叶节点的信息
            for (int i = 0; i < M; i++) {
                cin >> ID >> K;
                Node[ID].noKids = false;
    
                // 每个非叶节点 有K个子节点
                for (int j = 0; j < K; j++) {
                    cin >> son;
                    Node[son].father = ID;
                }
            }
            // 题目中说 根节点 是 1 
            // 从第一层开始 逐层向下建树(树的层次就是 节点 的 level, 并逐渐修改最大层数, )
            Node[1].level = 1;
            for (int i = 1; i <= N; i++) {
                for (int j = 1; j <= N; j++) {
                    if (i == Node[j].father) {
                        Node[j].level = Node[Node[j].father].level + 1;
                        if (Node[j].level > MaxLevel) {
                            MaxLevel = Node[j].level;
                        }
                    }
                }
            }
            // 统计每层没有子节点的节点的数量 
            for (int i = 1; i <= N; i++) {
                if (Node[i].noKids) {
                    level[Node[i].level] ++;
                }
            }
            for (int i = 1; i < MaxLevel; i++) {
                cout << level[i] << " ";
            }
            cout << level[MaxLevel] << endl;
        }
        return 0;
    }
  • 相关阅读:
    lintcode-453-将二叉树拆成链表
    qcow2虚拟磁盘映像转化为vmdk
    wiki
    oracle
    mysql配置记录
    consul命令记录
    Prometheus监控elasticsearch集群(以elasticsearch-6.4.2版本为例)
    centos7修改网卡名称为eth0
    linux下将普通用户加入到docker组,使它可以运行docker命令
    CentOS配置history记录每个用户执行过的命令
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/9451828.html
Copyright © 2011-2022 走看看