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. EF简单增删改查

    第一步:创建上下文对象 using(var db = new Entities()) { //数据操作 } 新增 UserInfo user = new UserInfo() { UserName = ...

  2. 1015 Reversible Primes

    1. 题目 2. 抽象建模 无 3. 方法 无 4. 注意点 素数判断(1不是素数) 数值的倒转 5. 代码 #include<stdio.h> #include<math.h> ...

  3. 消息中间件MQTT

    1.1概念 MQTT(MQ Telemetry Transport) 消息队列遥测传输协议是IBM开发的一种网络应用层的协议,提供轻量级的,支持可发布/可订阅的的消息推送模式,使设备对设备之间的短消息 ...

  4. HTML学习(9)头部

    HTML <head> 元素 <head> 元素包含了所有的头部标签元素.在 <head>元素中你可以插入脚本(scripts), 样式文件(CSS),及各种met ...

  5. 使用git pull同步github代码到服务器

    我直接用git pull的时候遇到这个错误: error: Your local changes to the following files would be overwritten by merg ...

  6. JSP读取数据库二进制图片并显示

    用JSP从数据库中读取二进制图片并显示在网页上 环境mysql+tomcat: 先在mysql下建立如下的表. 并存储了二进制图像(二进制格式存储图片可以参考我的另一篇博客:https://www.c ...

  7. Service Worker,Web Worker,WebSocket的对比

    Service Worker 处理网络请求的后台服务.适用于离线和后台同步数据或推送信息.不能直接和dom交互.通过postMessage方法交互. Web Worker 模拟多线程,允许复杂计算功能 ...

  8. CDH安装时,无法纳管全部的节点的一个bug

      问题描述: 使用CDH 5.2版本安装时,agent节点有12个.按照安装说明,在各个节点启动cm-agent之后,发现只有6个节点能被纳管.其它的节点总是无法加入纳管中. 在确认防火墙已经关闭后 ...

  9. LUA利用第三方API访问数据库

    ===========数据库访问--第三方 http { upstream backend { drizzle_server 192.168.4.119:3306 protocol=mysql dbn ...

  10. 每天进步一点点------Altium Designer集成库简介及创建

    一.集成库概述    Altium Designer 采用了集成库的概念.在集成库中的元件不仅具有原理图中代表元件的符号,还集成了相应的功能模块.如Foot Print 封装,电路仿真模块,信号完整性 ...