PAT-1004 Counting Leaves
1004 Counting Leaves (30 分)
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
算法说明:
算法要求求出每一层没有孩子节点的个数并依次输出,本文采用vector存储结点,当然你可以自己定义结构体。然后用递归计算每一层的没有孩子结点的个数,这时就必须有个数组book[depth]来记录啦,下标代表层数,对应的值表示这一层无孩子结点个数。递归停止的条件为 :vector[index].size()==0 说明当前结点没有孩子结点,使book[depth]++,下面贴出代码。
**树结构与vector存储**
```c++
// 1004 Counting Leaves.cpp : Defines the entry point for the console application.
//
include "stdafx.h"
include
include
using namespace std;
vector vec[100];
int maxDepth=-1;
int book[100]={0};
/**
4 2
01 2 02 03
02 1 04
*/
void dfs(int index,int depth){
if(vec[index].size()==0){
book[depth]++;
if(depth>maxDepth)
maxDepth=depth;
return;
}
for(int i=0;i<vec[index].size();i++)
dfs(vec[index].at(i),depth+1);
}
int main(int argc, char argv[])
{
int N,M,node,k,leaf;
cin >>N>>M;
for(int i=0;i<M;i++){
cin >>node>>k;
for(int j=0;j<k;j++){
cin >> leaf;
vec[node].push_back(leaf);
}
}
dfs(1,0);
cout<<book[0];
for(int j=1;j<=maxDepth;j++){
cout<<" "<<book[j];
}
return 0;
}
**<center>2020考研打卡第二天,你可知道星辰之变,骄阳岂是终点?千万不要小看一个人的决心。加油!!!</center>**
**<center>将来的你一定会感谢现在奋斗的自己</center>**
PAT-1004 Counting Leaves的更多相关文章
- 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 (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- 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 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 ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 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分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PTA (Advanced Level) 1004 Counting Leaves
Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...
随机推荐
- js 排序,去重
前几天 有一个需求要做一个 勾选的按钮 ,用的前端框架时 extjs . 需求是这样的:选择数据后点击勾选 会把数据 放到一个全局变量里,然后点击另外一个提交按钮 弹出一个窗口 加载这些已经勾选的 ...
- Office Web app server 2013 目前无法和windows server 2012 R2兼容。
另外旧版的office文档和PDF格式不支持预览功能.
- 页面元素固定在页面底部的纯css代码(兼容IE6)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- AlloyDesigner 使用
前端开发视觉是很重要的一部分,所以视觉的还原度很重要,今天给大家介绍一个很好用的视觉精度调整插件 一.下载AlloyDesigner插件 下载插件 二.保存视觉稿为图片格式 1.psd 用ps直接保存 ...
- PhpStorm破解版及使用教程
本文引自网络,仅供本人学习使用之用,感谢网友的分享 PhpStorm PhpStorm 是 JetBrains 公司开发的一款商业的 PHP 集成开发工具,旨在提高用户效率,可深刻理解用户的编码,提 ...
- Jenkins RCE(CVE-2018-1000861)
先说通过IDEA利用JPDA远程调试tomcat程序 在catalina.sh添加,或者catalina.bat内容不动用如下命令开启,默认是开启8000端口 set JAVA_OPTS=-Xdebu ...
- 使用docker-compose运行Django
1.新建空目录 2.进入该目录新建Dockerfile文件,并在该Dockerfile文件添加如下内容 FROM python:3 ENV PYTHONUNBUFFERED 1 RUN mkdir / ...
- Windows下安装Tensorflow—GPU版本
https://blog.csdn.net/weixin_39290638/article/details/80045236
- JavaScript HTML DOM,BOM
DOM DOM 是一个 W3C (万维网联盟) 标准. DOM 定义了用于访问文档的标准: "W3C 文档对象模型 (DOM) 是一个平台和与语言无关的界面, 允许程序和脚本动态访问和更新文 ...
- ceph存储osd启动异常处理和正常启停操作
机器角色:cloudstack虚拟机的宿主机:ceph存储机器. 事件:ceph存储的物理机器由于内存异常,需要停机更换,仅仅是把该物理机上面的虚拟机迁移走,同时启动了停机维护,然后就直接关机.结果造 ...