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

思路

输出每个高度叶子节点的个数。

深搜,搜到底后,使本高度的叶子节点数加一。

需要注意的是,我被卡了一组数据,因为我判断的是叶子节点的方法为mp[i].size() == 1

实际上,当根节点只有一个子节点时,也满足上述条件,因此需要特判一下。

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <map>
using namespace std; int N, M;
vector<int> mp[110];
int num[110];
int vis[110];
int maxd; void dfs(int index, int deep){
if(index != 1 && mp[index].size() == 1){
num[deep]++;
maxd = max(deep, maxd);
return;
} for(int i = 0; i < mp[index].size(); i++){
if(!vis[mp[index][i]]){
vis[mp[index][i]] = 1;
dfs(mp[index][i], deep + 1);
}
}
} int main(){
cin >> N >> M;
for(int i = 0; i < M; i++){
int id = 0, k = 0;
cin >> id >> k;
int t = 0;
for(int j = 0; j < k; j++){
cin >> t;
mp[t].push_back(id);
mp[id].push_back(t);
}
} vis[1] = 1;
dfs(1, 1); if(mp[1].size() == 0) cout << "1" << endl; for(int i = 1; i <= maxd; i++){
if(i == maxd)
cout << num[i];
else
cout << num[i] << " ";
}
// for(int i = 0; i < 100; i++){
// if(!mp[i].size()) continue;
// cout << i << " ";
// for(int j = 0; j < mp[i].size(); j++){
// cout << mp[i][j] << " ";
// }
// cout << endl;
// }
return 0;
}

PAT 1004 Counting Leaves (30分)的更多相关文章

  1. 1004 Counting Leaves (30分) DFS

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

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

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

  3. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  4. 1004 Counting Leaves (30 分)

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

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

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

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

  7. 1004. Counting Leaves (30)

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

  8. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  9. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

随机推荐

  1. Java-POJ1010-STAMP

    说良心话,题目不难,但是题目真的很不好懂,解读一下吧 题意: 读入分两行,第一行为邮票面额(面额相同也视为种类不同)以0结束,第二行为顾客要求的面额,以0结束 要求:每个顾客最多拿4张邮票,并求最优解 ...

  2. dbGet

    dbGet是Innovus/Encounter DBTCL命令的一种.除了dbGet,DBTCL的命令还包括以下几种: 1. dbSet 2. setDbGetMode/getDbGetMode 3. ...

  3. Spark学习笔记1

    趁着工作业余时间,趁着内心对技术追求的热情,还是对Spark这个大数据内存计算框架动手了,毕竟人与人之间的差距都是在工作业余时间拉开的…… Spark官网:http://spark.apache.or ...

  4. Flask 教程 第十九章:Docker容器上的部署

    本文翻译自The Flask Mega-Tutorial Part XIX: Deployment on Docker Containers 这是Flask Mega-Tutorial系列的第十九部分 ...

  5. Docker - Deepin中docker不能启动容器,-d也无效

    问题重现 1. 搭建mysql docker run -p 3306:3306 --name docker-mysql-5.7 -v $PWD/conf:/etc/mysql/conf.d -v $P ...

  6. buuctf 基础破解

    首先下载压缩包 解压之后发现是另一个压缩包 另一个压缩包上写着基础破解 然后用暴力破解的方法破解 然后搜了一下 暴力破解一般的长度是多少 然后搜到的结果是 长度为8的时候电脑都要破解好几年 所以我猜测 ...

  7. Nuxt 常用的配置项

    1:在开发项目时 我们可能会遇到端口被占用或者指定IP的情况, 在Nuxt中 我们可以在page.json 文件中进行配置,例如希望IP配置成125.0.0.1,端口设置1616 "conf ...

  8. 关于mybatis中sql映射文件模糊查询的使用

    1.从前台传递一个String类型的参数到后台进行查询,如果牵涉到模糊查询会报错,应该把参数封装到对象中再进行传递然后进行模糊查询 2.一个查询框,多个查询条件 <if test="c ...

  9. 在Docker中使用Microsoft SQL Server数据库

    下图中对SQL Server容器创建及数据库创建等操作进行了记录,方便自己日后查看.(文中的 * 仅表示隐藏自己的个人信息,手动马赛克,哈哈-) Docker下载可看上一篇博文mac系统,docker ...

  10. Atcoder Beginner Contest153F(模拟)

    应该也可以用线段树/树状数组区间更新怪兽的生命值来做 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespac ...