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 ...
随机推荐
- 20155217 2016-2017-2 《Java程序设计》第8周学习总结
20155217 2016-2017-2 <Java程序设计>第8周学习总结 教材学习内容总结 15.1日志 15.1.1日志API简介 java.util.logging包提供了日志功能 ...
- apache服务器yii2报The fileinfo PHP extension is not installed解决思路
这个问题整整困扰了我两天,今天终于搞定了.记录一下. 背景是这样的,我呢,在centos服务器上安装了lamp环境,其中php是5.3.3,在用composer安装yii2的时候,出现了某些yii2插 ...
- XSS姿势——文件上传XSS
XSS姿势--文件上传XSS 原文链接:http://brutelogic.com.br/blog/ 0x01 简单介绍 一个文件上传点是执行XSS应用程序的绝佳机会.很多网站都有用户权限上传个人资料 ...
- 20165227 实验三《敏捷开发与XP实践》实验报告
2017-2018-4 20165227 实验三<敏捷开发与XP实践>实验报告 实验内容 1.XP基础 2.XP核心实践 3.相关工具 实验要求 1.没有Linux基础的同学建议先学习&l ...
- RESTful Web 服务:教程
RESTful Web 服务:教程 随着 REST 成为大多数 Web 和 Mobile 应用的默认选择,势必要对它的基本原理有所了解. 在它提出十多年后的今天,REST 已经成为最重要的 Web ...
- vue总结07 常用插件
插件 开发插件 插件通常会为 Vue 添加全局功能.插件的范围没有限制——一般有下面几种: 添加全局方法或者属性,如: vue-custom-element 添加全局资源:指令/过滤器/过渡等,如 v ...
- 使用apt-get安装Nginx
Ubuntu 18.04,Nginx 1.14.0, 一直想在Linux上安装Nginx,一直没找到契机,很大原因是自己不熟悉,Ubuntu没安装好吧!今天下午学习了Ubuntu安装软件的一些资料,那 ...
- 洛谷P1168中位数
传送门啦 基本思想就是二分寻找答案,然后用树状数组去维护有几个比这个二分出来的值大,然后就没有了: 数据要离散,这个好像用map也可以,但是不会: 那怎么离散呢? 我们先把a数组读入并复制给s数组,然 ...
- Codeforces 946D Timetable(预处理+分组背包)
题目链接:http://codeforces.com/problemset/problem/946/D 题目大意:有n个字符串,代表n天的课表,1表示这个时间要上课,0表示不要上课,一天在学校时间为第 ...
- SQLSERVER中的人民币数字转大写的函数实现
CREATE FUNCTION [dbo].[f_num_chn] (@num numeric(14,5))RETURNS varchar(100) WITH ENCRYPTIONASBEGIN-- ...