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 ...
随机推荐
- MongoDB资料汇总专题(转)
原文地址:http://blog.nosqlfan.com/html/3548.html 1.MongoDB是什么 MongoDB介绍PPT分享 MongoDB GridFS介绍PPT两则 初识 Mo ...
- 在html中显示Flash的代码
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...
- 【BZOJ4950】lydsy七月月赛 C 二分图最大匹配
[BZOJ4950]lydsy七月月赛 C 题面 题解:比较直接的想法就是:每行,每列的最大值都留下,剩下的格子都变成1.但是如果一个格子既是行的最大值又是列的最大值,那么我们只需要把它留下即可.这就 ...
- FICO credit score
http://www.bankrate.com/finance/credit/what-is-a-fico-score.aspx Anyone who’s ever thought about loo ...
- Android笔记之权限库AndPermission
GitHub地址:https://github.com/yanzhenjie/AndPermission 这个库可以节省不少代码量和时间 使用示例如下 findViewById(R.id.btnGet ...
- linux命令行快捷键记录
摘自: http://www.cnblogs.com/webzhangnan/p/3221410.html [移动光标] Ctrl+A 标移到行首.它在多数文本编辑器和 Mozilla 的 URL 字 ...
- Objective-C学习之解析XML
通过soap请求webservice时,返回的数据是XML类型,有时候也需要解析本地的xml数据等,苹果自带类NSXMLParser解析xml还是很方便的,简单轻便 本文以解析本地XML为例,网络获取 ...
- BZOJ 1628 [Usaco2007 Demo]City skyline:单调栈
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1628 题意: 题解: 单调栈. 单调性: 栈内元素高度递增. 一旦出现比栈顶小的元素,则表 ...
- centos服务器安装配置Postgre9.6
安装: STEP1:下载对应rpm yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64 ...
- JavaScript实现按键记录,并在关掉网页之前把记录的内容post出去
最近陈老师让我给新架构加一个按键记录的业务.去学习了JavaScript,网上找了一些代码,最后写出来了: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTM ...