当前位置: 首页 > news >正文

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。我处理的思路为先存下每个节点的孩子。然后遍历的时候,寻找出每一层的节点,如果该节点没有孩子了,就说明为叶子节点。这样循环遍历就能求出所有的节点。

#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;
}

http://www.lryc.cn/news/125298.html

相关文章:

  • 最长递增子序列——力扣300
  • 邮递员送信 单源最短路+反向建边
  • git的常用操作
  • vscode搭建java开发环境
  • 01 qt快速入门
  • 嵌入式开发中常用且杂散的命令
  • JS导出复杂多级表头的Excel
  • 2023国赛数学建模E题思路分析
  • 【JavaScript 12】二进制位运算符 或 与 非 异或 左移 右移 头部补零右移
  • Kafka 入门到起飞 - Kafka是怎么保证可靠性的呢
  • 数学建模(三)整数规划
  • 全面梳理Python下的NLP 库
  • 系统设计类题目汇总三
  • “深入解析JVM:探索Java虚拟机的内部工作原理“
  • VB+sql小型超市管理系统设计与实现
  • mysql面试
  • 3.1 Ansible 的使用和配置管理
  • 神经网络基础-神经网络补充概念-06-计算图
  • 【【STM32之GPIO】】
  • 【动画】p60动画蓝图、播放蒙太奇、打包
  • 去趋势化一个心电图信号、信号功率谱、低通IIR滤波器并平滑信号、对滤波器引起的延迟进行补偿研究(Matlab代码实现)
  • NTN(六) switchover
  • Ceph三个接口的创建
  • 接口测试和功能测试的区别
  • LeetCode 1572. 矩阵对角线元素的和
  • SQLSERVER 查询语句加with (NOLOCK) 报ORDER BY 报错 除非另外还指定了 TOP、OFFSET 或 FOR XML
  • 创建react native项目的笔记
  • Java自动化测试之Chrome网页爬取
  • boost下的asio异步高并发tcp服务器搭建
  • HCIP第五节------------------------------------------ospf