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, the number of nodes in a tree, and M (<), 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
DFS:因为要知道有多少层,每次进DFS先更新一下最大层数
#include <bits/stdc++.h>
using namespace std;
int n,k,id,m,k2;
vector<int> v[];
int num[];
int maxn = ;
void find(int a,int b)
{
maxn = max(maxn,b);
if(v[a].size() == ){
num[b] += ;
return ;
}
else{
for(int i=;i<v[a].size();i++){
find(v[a][i],b+);
}
}
}
int main()
{
scanf("%d %d",&n,&m);
memset(num,,sizeof(num));
for(int i=;i<=m;i++)
{
scanf("%d",&id);
scanf("%d",&k);
for(int j=;j<k;j++)
{
scanf("%d",&k2);
v[id].push_back(k2);
}
}
find(,);
for(int i=;i<=maxn;i++)
{
if(i==){
printf("%d",num[i]);
}
else{
printf(" %d",num[i]);
}
}
return ;
}
1004 Counting Leaves (30 分)的更多相关文章
- 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分) 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 ...
- 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 ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 1004. Counting Leaves (30)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family membe ...
- 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 ...
随机推荐
- mysql 中遇到金额 BigDecimal类型字段
当数据库字段为BigDecimal型,and后面判断去掉. 否则当为0时候,视为空处理了.即传参为null
- vue 生命周期钩子
每个vue实例被创建时都会经历一系列初始化的过程,像是一个生命从无到有的过程,所以叫生命周期,而这个过程都有对应的不同阶段,也就对应了生命周期不同的钩子函数,这些生命周期函数,作为vue实例的属性使用 ...
- Server.xml配置详解
<Server port="8005" shutdown="SHUTDOWN"> <!-- 属性说明 port:指定一个端口,这个端口负责监听 ...
- ABAP 动态内标排序
动态内表怎样排序动态内表怎样排序 动态内表要排序时,因为不知道内表中的字段名字,所以不能直接用SORT table BY field1 field2... 可以使用下面的方法来实现:SORT tab ...
- Chain of Responsibility Pattern
1.Chain of Responsibility模式:将可能处理一个请求的对象链接成一个链,并将请求在这个链上传递,直到有对象处理该请求(可能需要提供一个默认处理所有请求的类,例如MFC中的Cwin ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...
- Android studio 添加assets文件夹
我们知道Eclipse创建的工程默认是有个assets文件夹的,但是Android studio默认没有帮我们创建,那么我们就自己创建一个就好啦. (1)手动创建 在项目的顶部有个下拉,默认选择的是A ...
- the art of seo(chapter three)
SEO Planning: Customizing Your Strategy ***Developing an SEO Plan Prior to Site Development***Determ ...
- php字符串啊的heredoc格式
Heredoc技术,在正规的PHP文档中和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术.它也出现unix/linux的shell编程里面.但是现在的一些论坛程序,和部分文 ...
- hdu 6058
\(f(l,r,k)=\)区间[\(l\),\(r\)]的第k大. \(\sum_{l=1}^{n}{\sum_{r=l}^{n}{f(l,r,k)}}\) 参考题解,claris大佬题解.赛后AC. ...