PAT (Advanced Level) 甲级 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 0 leaf node; and on the next level, there is 1 leaf node. Then we should output 0 1 in a line.

这个作为一个30的题,感觉也是很简单的。题目大意就是给出每个非叶子节点的所有孩子,然后求出该树的每一层的叶子节点。已知根节点为1。我处理的思路为先存下每个节点的孩子。然后遍历的时候,寻找出每一层的节点,如果该节点没有孩子了,就说明为叶子节点。这样循环遍历就能求出所有的节点。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;

const int maxn = 106;

//建一个树 求每一层的叶子节点个数 01为根节点

map<int,vector<int> >node;

map<int,vector<int> >vc;//表示前几层 每层的节点
int ans[maxn];
int main()
{
    int N,M;cin >> N >> M;
    for(int i = 0;i<M;i++)
    {
        int rt,k;cin >> rt >> k;
        for(int j = 0;j<k;j++)
        {
            int x;cin >> x;
            node[rt].push_back(x);
        }
    }
    vc[1].push_back(1);//表示根节点只有1 也是第一层
    int lim = 0;
    for(int i = 2;i<100;i++)
    {
        for(int j = 0;j<vc[i-1].size();j++)//拿出上一层节点
        {
            int nw = vc[i-1][j];
            for(int k = 0;k<node[nw].size();k++)
            {
                int ci = node[nw][k];
                vc[i].push_back(ci);
                if(node[ci].size()==0)ans[i]++;//当层存在空姐点
            }
        }
        if(vc[i].size()==ans[i])
        {
            lim = i;
            break;
        }
    }
    if(N==0);
    else if(N==1)
    {
        cout << 1;
    }else {
        cout << 0;
        for(int i = 2;i<=lim;i++)
        {
            cout<<" " << ans[i];
        }
    }
    
    
    return 0;
}
相关推荐
2401_841495645 小时前
【数据结构】红黑树的基本操作
java·数据结构·c++·python·算法·红黑树·二叉搜索树
西猫雷婶5 小时前
random.shuffle()函数随机打乱数据
开发语言·pytorch·python·学习·算法·线性回归·numpy
小李独爱秋5 小时前
机器学习中的聚类理论与K-means算法详解
人工智能·算法·机器学习·支持向量机·kmeans·聚类
liu****6 小时前
负载均衡式的在线OJ项目编写(六)
运维·c++·负载均衡·个人开发
青草地溪水旁6 小时前
设计模式(C++)详解——迭代器模式(3)
c++·设计模式·迭代器模式
奔跑吧邓邓子7 小时前
【C++实战㊺】解锁C++代理模式:从理论到实战的深度剖析
c++·实战·代理模式
杜子不疼.7 小时前
【C++】玩转模板:进阶之路
java·开发语言·c++
夜晚中的人海7 小时前
【C++】异常介绍
android·java·c++
m0_552200827 小时前
《UE5_C++多人TPS完整教程》学习笔记60 ——《P61 开火蒙太奇(Fire Montage)》
c++·游戏·ue5
charlie1145141917 小时前
精读C++20设计模式——行为型设计模式:迭代器模式
c++·学习·设计模式·迭代器模式·c++20