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入门3 —— 基本数据类型
			一:为何变量值(记录的数据)要有类型呢? 1.既然可以记录事物的状态,为什么要分类型呢? 变量值是来记录事物状态的, 而事物的状态是多种多样的, 所以对应着就要应该用不同类型的值去记录这些状态. 二: ... 
- mysql 实践(例题)
			MySQL安装见本博 安装成功后,开始菜单中找到 “MySQL 8.0 Command Line Client” 进行启动(启动后,可直接输入MySQL密码) 1. create database 数 ... 
- [Netcat] 断线重连 自动重连
			今天想做些操作,所以想到了nc 但是nc太过于轻量级 所以 导致我没有找到他的断线重连功能 然后我就想到了windows的神器之一 vbs脚本 vbs代码如下 Dim a,b set a= WScri ... 
- python-第三方库的理解及某个函数的源代码
			第三方库,是一个总称,里面有各个模块,而具体使用的函数是模块里的. 库包含多个模块, 每个模块里包含多个函数. import AAAA 就是引用AAAA这个库,这个库里的模块函数都可以用,只是 ... 
- RegExp-named captured groups(命名分组捕获)
			console.log('2020-01-23'.match(/(\d{4})-(\d{2})-(\d{2})/)) const t = '2020-01-23'.match(/(?<year& ... 
- python中列表常用的几个操作函数
			# coding=utf-8#在列表末尾添加新的对像#实例展现函数append()的用法aList=[456,'abc','zara','ijk',2018]aList.append(123)prin ... 
- VSCode配置FTP
			首先在VScode中下载安装插件SFTP Windwos下摁Ctrl+Shift+P,输入SFTP: config命令并运行,进入sftp.json配置项如下: { "name": ... 
- SQLite 3 中的数据类型
			SQLite使用动态类型系统,在SQLite中,值的数据类型和值本身,而不是和它的容器,关联在一起的.SQLite的动态类型系统和其他数据库引擎的静态类型系统是兼容的,这样在静态类型的数据库上执行的S ... 
- .net C#  Chart控件的简单使用
			1.拖控件Chart 到界面 2. 清除默认的序列 chart1.Series.Clear(); 3.生成一个序列,并添加到chart1中,序列可添加多个 Series s1 = new Se ... 
- Plastic Bottle Manufacturer Tips: Use Caution For Plastic Bottles
			Plastic bottles use polyester (PET), polyethylene (PE), polypropylene (PP) as raw materials, after a ... 
