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 ...
随机推荐
- opencv的基本数据结构(二)(转)
转自:原文链接,以下代码.图片.内容有点改动,只为转载不降低博客内容的可阅性,版权归原作者所有. OpenCV中强大的Mat类型大家已经比较熟悉了.这里梳理一些在工程中其他经常用到的几种基本数据类型. ...
- mysql关于“异步复制”“同步复制”“半同步复制”“无损复制”的概念与区别
异步复制:主把事务写到binlog日志并不管从是否接收或者什么时候接收,commit之后,不会与从发生ack之类的交互. 同步复制:当主提交一个事务,在主向前端返回一个commit成功的指令前,必须保 ...
- 15、BigDecimal类简介
BigDecimal类概述 由于在运算的时候,float类型和double很容易丢失精度,在金融.银行等对数值精度要求非常高的领域里面,就不能使用float或double了,为了能精确的表示.计算浮点 ...
- spfa+差分约束系统(C - House Man HDU - 3440 )+对差分约束系统的初步理解
题目链接:https://cn.vjudge.net/contest/276233#problem/C 题目大意:有n层楼,给你每个楼的高度,和这个人单次的最大跳跃距离m,两个楼之间的距离最小是1,但 ...
- 【算法学习】manacher
manacher太水了. 这篇blog不能称作算法学习,因为根本没有介绍…… 就贴个模板,太简单了…… #include<cstdio> #include<cstring> # ...
- MFC将二进制文件导入资源后释放
1.前言 前一篇笔记记录了怎么修改PE,此篇记录下如何利用自身的资源文件. 2.编程思路 获得资源句柄 - 获得资源文件大小 - 加载资源文件 - 锁定资源并获得其指针. 3.实践代码 1)编译以下代 ...
- linux===sar命令性能监控
sar介绍: sar是System Activity Reporter(系统活动情况报告)的缩写.sar工具将对系统当前的状态进行取样,然后通过计算数据和比例来表达系统的当前运行状态.它的特点是可以连 ...
- BigDecimal常用方法
一.介绍 Java中提供了大数字(超过16位有效位)的操作类,即 java.math.BinInteger 类和 java.math.BigDecimal 类,用于高精度计算. 其中 BigInteg ...
- mac 升级10.12 php debug 环境 跑不起的解决 解决方案
1: mac 升级后发现 php从原来的5.5 升级为 5.6 了... 所以以前 php.ini 里面的配置全部都没有了. mac 给我们做了备份2: 没办法只能升级php对应的插件到5. ...
- Python 模块进阶
import导入模块 1. import 搜索路径 import sys sys.path 例子: In [1]: import sys In [2]: sys.path Out[2]: ['', ' ...