PAT-甲级-1004 Counting Leaves C++

发布时间 2023-07-11 15:22:36作者: 正明小佐

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 , the number of nodes in a tree, and  (), the number of non-leaf nodes. Then  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  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 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

题意:

题目意思是按照树的高度,从根节点(level 0)开始,从上往下输出没有叶子节点的个数。

分析:

用Tree来描述节点,用一个vector来存储所有节点。处理输入的过程中确定每一个节点的孩子节点的情况。然后用BFS遍历这棵树,确定每个节点的高度,最后输出。

代码:

//
// Created by yaodong on 2023/7/11.
//
#include <vector>
#include "iostream"
#include "cstring"
#include "queue"
class Tree{
public:
    int index;                // 在数组中的位置
    int num;                  // 孩子节点的个数
    int depth;                // 节点所处高度
    std::vector<int> child;    // 记录孩子节点的在数组中的位置—index
    Tree(int index){
        this->index = index;
        this->num = 0;
        this->depth = 0;
    }
    Tree(int index, int num, int childArray[]){
        this->index = index;
        this->num = num;
        for (int i = 0; i < num; ++i)
            this->child.push_back(childArray[i]);
    }
};

int main() {
    int n, m;
    std::cin >> n >> m;
    std::vector<Tree> treeVector;
    for (int i = 0; i < n; ++i) {
        treeVector.emplace_back(i);
    }
//    printf("%d\n", treeVector.size());
    for (int i = 0; i < m; ++i) {
        int parentId;
        std::cin >> parentId;
        Tree &curNode = treeVector[parentId-1];
        int k;
        std::cin >> k;
        curNode.num = k;
        for (int j = 0; j < k; ++j) {
            int temp;
            std::cin >> temp;
            curNode.child.push_back(temp-1);
        }
    }
    int noLeafDepth[n];
    memset(noLeafDepth, 0, sizeof(noLeafDepth));
    std::queue<int> q;        //用队列的性质来处理depth,从根节点0开始
    q.push(0);
    while(!q.empty()){
        int first = q.front();
        q.pop();
        Tree &t = treeVector[first];
        for (int i = 0; i < t.num; ++i) {
            int childIndex = t.child[i];
            treeVector[childIndex].depth = t.depth + 1;
            q.push(childIndex);
        }
    }
    int maxDepth = -1;
    for (int i = 0; i < n; ++i) {
        Tree t = treeVector[i];
        if(t.depth > maxDepth)
            maxDepth = t.depth;
        if(t.num == 0)
            noLeafDepth[t.depth]++;
    }
//    printf("maxDepth: %d\n", maxDepth);
    for (int i = 0; i <= maxDepth; ++i) {
        if(i == 0)
            printf("%d", noLeafDepth[i]);
        else
            printf(" %d", noLeafDepth[i]);
    }
}