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))的更多相关文章

  1. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

  2. 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 ...

  3. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  4. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  5. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  6. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. PAT甲1004 Counting Leaves【dfs】

    1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...

  8. 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 ...

  9. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

  10. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

    题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...

随机推荐

  1. 【leetcode 简单】 第九十八题 第三大的数

    给定一个非空数组,返回此数组中第三大的数.如果不存在,则返回数组中最大的数.要求算法时间复杂度必须是O(n). 示例 1: 输入: [3, 2, 1] 输出: 1 解释: 第三大的数是 1. 示例 2 ...

  2. HTML字体的设置

    CSS字体设置 box-sizing:border #content-box   box-shadow:设置盒子边框的阴影.     字体动作:   font-family:设置字体.比如:‘微软雅黑 ...

  3. JavaScript中函数参数的值传递和引用传递

    结论: 对于数字.字符串等基本类型变量,是将它们的值传递给了函数参数,函数参数的改变不会影响函数外部的变量. 对于数组和对象等是将对象(数组)的变量的值传递给了函数参数,这个变量保存的指向对象(数组) ...

  4. 浅析XSS与XSSI异同

    浅析XSS与XSSI异同 这篇文章主要介绍了XSS与XSSI异同,跨站脚本(XSS)和跨站脚本包含(XSSI)之间的区别是什么?防御方法有什么不同?感兴趣的小伙伴们可以参考一下 Michael Cob ...

  5. solr4.10.3部署到tomcat——(十)

    0. 准备环境:

  6. aarch64_g3

    glibc-langpack-wal-2.25-6.fc26.aarch64.rpm 2017-06-20 17:08 210K fedora Mirroring Project glibc-lang ...

  7. 服务发现 consul cluster 的搭建

    consul cluster setup 介绍和指南: consul用于服务发现.当底层服务发生变化时,能及时更新正确的mysql服务IP. 并提供给业务查询.但需要自行编写脚本,监测数据库状态和切断 ...

  8. MySQL 5.7.17 Group Relication(组复制)搭建手册【转】

    本博文介绍了Group Replication的两种工作模式的架构.并详细介绍了Single-Master Mode的部署过程,以及如何切换到Multi-Master Mode.当然,文末给出了Gro ...

  9. javascript按照指定格式获取上一个月的日期

    //get pre month//get pre month function getPreMonth() { var date=new Date().Format("yyyy-MM-dd& ...

  10. 两个Bounding Box的IOU计算代码

    Bounding Box的数据结构为(xmin,ymin,xmax,ymax) 输入:box1,box2 输出:IOU值 import numpy as np def iou(box1,box2): ...