1004 Counting Leaves (30)(30 point(s))
problem
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input
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.
Output
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
tip
一颗树,找到每层的叶子数量。
anwser
#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define Max 101
#define mp(x, y) std::make_pair(x, y)
int N, M, level[Max], maxLevel = 0;
std::vector<int > kids[Max];
//typedef std::pair<int ,int> node
int main(){
// freopen("test.txt", "r", stdin);
memset(level, 0, sizeof(level));
std::cin>>N>>M;
int fa, kidNum, kid;
for(int i = 0; i < M; i++){
std::cin>>fa>>kidNum;
for(int j = 0; j < kidNum; j++){
std::cin>>kid;
kids[fa].push_back(kid);
// std::cout<<kid<<" ";
}
// std::cout<<std::endl;
}
// for(int i = 1; i <= N; i++) {
// if(i == 1)std::cout<< kids[i].size();
// else std::cout<<" "<<kids[i].size();
// }
std::queue<std::pair<int, int> > que;
que.push(mp(1,0));
while(!que.empty()){
std::pair<int, int> father = que.front(); que.pop();
if(kids[father.first].size() == 0) {
// std::cout<<father.first<<std::endl;
level[father.second] ++;
maxLevel = father.second;
continue;
}
for(int i = 0; i < kids[father.first].size(); i++){
que.push(mp(kids[father.first][i], father.second+1));
}
}
for(int i = 0; i <= maxLevel; i++)
{
if (i == 0) std::cout<<level[i];
else std::cout<<" "<<level[i];
}
return 0;
}
/*
4 2
01 1 02
02 2 03 04
*/
experience
英语单词:
- hierarchy 层级关系
1004 Counting Leaves (30)(30 point(s))的更多相关文章
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 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 (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甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- A1095 Cars on Campus (30)(30 分)
A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...
- 1004 Counting Leaves ——PAT甲级真题
1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...
- 【PAT甲级】1004 Counting Leaves (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
随机推荐
- STL-map and multimap
定义 map<int,int> a; map<int,string> a; map<double,int> a; …… 首先要知道的一些 Map是STL的一个关联容 ...
- Shell脚本-自动化部署反向代理、WEB、nfs
部署nginx反向代理三个web服务,调度算法使用加权轮询(由于物理原因只开启两台服务器) AutoNginxNfsService.sh #/bin/bash systemctl status ngi ...
- Android获取手机分辨率DisplayMetircs类
关于Android中手机分辨率的使用 Android 可设置为随着窗口大小调整缩放比例,但即便如此,手机程序设计人员还是必须知道手机屏幕的边界,以避免缩放造成的布局变形问题. 手机的分辨率信息是手机的 ...
- [Ubuntu 14.04] 创建可以用于Android的WIFI热点
Ubuntu的网络管理为创建Wifi热点提供了方便,可是因为它用了ad-hoc网络,所以其创建的Wifi又不能让Android系统使用.这篇文字就是为了解决这个问题 1.Install AP-Host ...
- [Ubuntu 14.04] 安装Flash && 安装QQ
一.安装Flash 打开Firefox浏览器弹出的Flash安装提醒早都烦死了,那么Ubuntu14.04怎么安装Flash呢? 1.32位系统命令行安装: 第一步 更新库: sudo apt-get ...
- 【codeforces】【比赛题解】#940 CF Round #466 (Div. 2)
人生的大起大落莫过如此,下一场我一定要回紫. [A]Points on the line 题意: 一个直线上有\(n\)个点,要求去掉最少的点,使得最远两点距离不超过\(d\). 题解: 暴力两重fo ...
- nginx自定义500,502,504错误页面无法跳转【转】
1.自定一个页面,这个页面是一个链接地址可以直接访问的. 以下是nginx的配置: location / { proxy_pass http://tomcat_app108; ...
- python3 切换工作文件夹
python3 默认的工作文件夹在Python安装路径下.如下为查看工作文件夹路径: >>> import os >>> os.getcwd() 'D:\\Work ...
- shell系统检测->
系统状态检测脚本练习 1-> 查看磁盘状态 思路:查看磁盘/当前使用状态,如果使用率超过80%则报警发邮件 1.获取磁盘当前使用的值 df -h|grep /$ 2.从获取到的值中提取出,对应的 ...
- Runtime.getRuntime().exec 类 防止阻塞
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...