首页 > 编程语言 >PAT-甲级-1004 Counting Leaves C++

PAT-甲级-1004 Counting Leaves C++

时间:2023-07-11 15:22:49浏览次数:43  
标签:index PAT treeVector int Leaves depth num 1004 节点

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 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]);
    }
}

 

标签:index,PAT,treeVector,int,Leaves,depth,num,1004,节点
From: https://www.cnblogs.com/langweixianszu/p/17544773.html

相关文章

  • Datapath编码方式
    (5条消息)Datapath综合代码规范(Verilog)_沧海一升的博客-CSDN博客Datapath综合的编码准则-Synopsys-百度文库(baidu.com)......
  • CF1702G2 Passable Paths (hard version)
    PassablePaths(hardversion)思路题意:判断是否存在一条链包含树上给定点集。考虑把\(1\)当做树的根,将无根树转化为有根树。考虑这样一个性质:若存在满足条件的最短链,则点集中深度最深的点\(u\)是该链的一个端点,点集中距离\(u\)最远的点\(v\)是该链的另一端点。证明......
  • 【git】代码patch包生成和合入
    patch合入gitamgitam会直接将patch的所有信息打上去,而且不用重新gitadd和gitcommit,author也是patch的author而不是打patch的人常用命令gitam0001-limit-log-function.patch#将名字为0001-limit-log-function.patch的patch打上gitam--signoff0001-limit-......
  • ML-for-AGV-Dispatching:run.py解读
    importsimpyfromShopFloorimportCenterimportLearnasLeimportnumpyasnpimportpandasaspdimportpickleimporttimeTask="None"#TestD,TestQ,TestDQN,TestSVM,CollectV,CollectW,TrainQ,TrainDQNTask="TestD"'�......
  • UI PATH
     最最最最重要的创建变量时,尽量不要自己在下方创建,而是在需要变量的地方,在属性CreateVariable这里创建变量。 ImageExists- 图片存在:使用ImageExists识别页面某块区域的内容,是否如选取图片所示,如果匹配成功返回True,否则返回False。 提取页面某块内容信息,使用getfull......
  • 让python的lxml模块的xpath支持正则表达式
    python的lxml模块是处理xml文档的比较好用的工具,其中的xpath函数可以检索指定的元素,但是它不支持正则表达式,比如某个属性的值是否匹配某个正则表达式,就没有办法实现.不过可以利用它的自定义函数扩展功能来实现,如下代码所示:importrefromlxmlimportetreefromlxm......
  • PAT乙级【Java题解合集】
    ✨说在前面       这个暑假博主用大概两周不到的闲暇时间把PAT乙级的110道算法题全部肝完了,个人感觉题目的难度大部分在中等偏下,大概有二十道左右的题目还是蛮有意思的,值得细细去钻研,本专栏非常适合新手入门算法,也适合Java算法老手巩固一些基本知识点,由于C站上关于PAT乙级J......
  • Codeforces 293B Distinct Paths
    发现\(n,m\)的数据范围是假的,因为每一步一个颜色最多也就\(k\le10\)种颜色,所以当\(n+m-1>k\)时一定无解。接下来发现这个数据范围挺小的,考虑状压,设\(f_{x,y}\)为走到\((x,y)\)点所用的颜色的集合,其可以由\(f_{x-1,y},f_{x,y-1}\)推来。然后就可以......
  • OGG-02912 Patch 17030189 is required on your Oracle mining database for trail fo
    Therewillbeascript"prvtlmpg.plb"undergghomedirectory[oracle@OGGR2-1ogg]$ls-lrtprvtlmpg.plb-rw-r-----1oracleoinstall9487May272015prvtlmpg.plb[oracle@OGGR2-1ogg]$pwd/ogg[oracle@OGGR2-1ogg]$Logintothedatabaseand......
  • 【CF1797F】Li Hua and Path
    于2023.5.10更新:更正了两处笔误。考虑如下定义:\(A\)表示满足第一种路径的\((u,v)\)集合。\(B\)表示满足第二种路径的\((u,v)\)集合。\(C\)表示满足前两种路径的\((u,v)\)集合。然后答案显然就是\(|A|+|B|-2|C|\)。先求出这一类的答案,再解决动态挂叶子的问......