PAT 1004 Counting Leaves (30分)
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分)的更多相关文章
- 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 (30 分)(BFS)
题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- 1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...
- 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 ...
- 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)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- python之路之生成器和的迭代器
生成器的基本原理 生成器实现xrange 迭代器
- 1069 The Black Hole of Numbers (20分)
1069 The Black Hole of Numbers (20分) 1. 题目 2. 思路 把输入的数字作为字符串,调用排序算法,求最大最小 3. 注意点 输入的数字的范围是(0, 104), ...
- Java数据处理,Map中数据转double并取小数点后两位
BigDecimal order = (BigDecimal) map.get("finishrat"); double d = (order == null ? 0 : orde ...
- B1027 打印沙漏
题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805294251491328 1027 打印沙漏 (20 分) ...
- 题解【洛谷P2615】[NOIP2015]神奇的幻方
题目描述 幻方是一种很神奇的 \(N \times N\) 矩阵:它由数字 \(1,2,3,\cdots \cdots ,N \times N\) 构成,且每行.每列及两条对角线上的数字之和都相同. ...
- Documents
centos 7 修改主机名 hostnamectl set-hostname myhostname ansible node -m raw -a "if [[ \$(cat /root/. ...
- Win10 系统运行VsCode出现白屏的问题(亲测有效)
Win10 系统运行VsCode出现白屏的问题(亲测有效) 新买的本本,昨天VScode运行还正常,今天打开一直白屏,什么都没有,只有几个小格格,也不是卡死的那种,可以轻松关闭, 刚开始以为版本问题, ...
- redis持久化优缺点
- drf三大组件之频率认证组件
复习 """ 1.认证组件:校验认证字符串,得到request.user 没有认证字符串,直接放回None,游客 有认证字符串,但认证失败抛异常,非法用户 有认证字符串, ...
- linux安装、使用优化、常用软件
定制自己的ubuntu桌面系统 一.安装ubuntu 1.下载ubuntu镜像Iso文件 ubuntu官网下载:https://cn.ubuntu.com/download 2.u盘写入 (1)下载U ...