PTA 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, the number of nodes in a tree, and M (<), the number of non-leaf nodes. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]
where
IDis a two-digit number representing a given non-leaf node,Kis the number of its children, followed by a sequence of two-digitID's of its children. For the sake of simplicity, let us fix the root ID to be01.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
01is the root and02is its only child. Hence on the root01level, there is0leaf node; and on the next level, there is1leaf node. Then we should output0 1in a line.Sample Input:
2 1
01 1 02
Sample Output:
0 1
这题没什么难的,可能存父节点不存子节点难想一点,但事实上存子节点也不难,总之随便水水就好。
代码:
#include <iostream>
#include <string>
using namespace std; struct NODE{
int father,depth;
bool nochild;
}; int main(){
int n,m,father_id,son_id,son_number,res[]={},depth_max;
NODE tree[];
for (int i=;i<=;i++){
tree[i].father=tree[i].depth=;
tree[i].nochild=true;
}
cin >> n >> m;
for (int i=;i<m;i++){
cin >> father_id >> son_number;
if (son_number!=) tree[father_id].nochild=false;
for (int j=;j<son_number;j++){
cin >> son_id;
tree[son_id].father=father_id;
}
}
for (int i=;i<=n;i++){
int now=i,level=;
for (;tree[now].father!=;){
now=tree[now].father;
level++;
}
tree[i].depth=level;
depth_max=(depth_max>level)?depth_max:level;
}
for (int i=;i<=n;i++){
if (tree[i].nochild) res[tree[i].depth]++;
}
for (int i=;i<depth_max;i++)
cout << res[i] << " ";
cout << res[depth_max];
return ;
}
PTA 1004 Counting Leaves的更多相关文章
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
随机推荐
- NR / 5G - The Proportional Fair algorithm
- 地址ip丢失
好好的机器 ip地址没有了,好几台机器出现这个情况,目前判断是 NetworkManager 的问题: 先了解一下network和NetworkManager之间的区别,还有他们分别代表了什么. ...
- spark sql 查询hive表并写入到PG中
import java.sql.DriverManager import java.util.Properties import com.zhaopin.tools.{DateUtils, TextU ...
- Kafka运维大全来了!优化、监控、故障处理……
Kafka概念 Kafka是分布式发布-订阅消息系统.它最初由LinkedIn公司开发,之后成为Apache项目的一部分.Kafka是一个分布式的.可划分的.冗余备份的.持久性的日志服务.它主 ...
- 杭电1007-----C语言实现
这道题花了好久的时间才做出来,刚开始没有思路,最后看了网上的解答,好难得样子,每次都没有看完,但是掌握了大概思想,今天试着做了一下,已ac 主要思想:先将点对按照x排序,再在x排好序的基础上按照y来排 ...
- 深浅COPY浅谈
通俗地谈一下深浅COPY的区别,顺便做个笔记.(纯手打,如有不对之处还望大牛指导改正.) 个人觉得区分这个概念需要从数据类型来切入: 1.如果“被复制对象”(后面简称“原对象”)的数据类型为不可变数据 ...
- 戏说前端之CSS编码规范
前言 项目启动时 css 应该注意哪些问题 文件名规范 文件名建议用小写字母加中横线的方式.为什么呢?因为这样可读性比较强,看起来比较清爽,像链接也是用这样的方式,例如 // 地址: github的地 ...
- kuangbin专题 专题九 连通图 Warm up HDU - 4612
题目链接:https://vjudge.net/problem/HDU-4612 题目:一个大地图,给定若干个连通图,每个连通图中有若干个桥,你可以在任意某个连通图的 任意两个点添加一条边,问,添加一 ...
- Zabbix3.4使用详解
zabbix-基础 第1章 关于zabbix 1.1 为什么要使用监控 1.对系统不间断实时监控2.实时反馈系统当前状态3.保证服务可靠性安全性4.保证业务持续稳定运行 1.2 如何进行监控 比如我们 ...
- 服务器控件Repeater
在使用aspx开发中,如果一个页面做纯数据展示,Repeater会比GridView更适合,因为它是轻量级的 下面是最基本的用法: aspx: <table> <asp:Repea ...