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 分)的更多相关文章

  1. PAT 1004 Counting Leaves (30分)

    1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  2. 1004 Counting Leaves (30分) DFS

    1004 Counting Leaves (30分)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  3. 【PAT甲级】1004 Counting Leaves (30 分)(BFS)

    题意:给出一棵树的点数N,输入M行,每行输入父亲节点An,儿子个数n,和a1,a2,...,an(儿子结点编号),从根节点层级向下依次输出当前层级叶子结点个数,用空格隔开.(0<N<100 ...

  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. 1004. Counting Leaves (30)

    1004. Counting Leaves (30)   A family hierarchy is usually presented by a pedigree tree. Your job is ...

  7. PAT 解题报告 1004. Counting Leaves (30)

    1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...

  8. PAT 1004. Counting Leaves (30)

    A family hierarchy is usually presented by a pedigree tree.  Your job is to count those family membe ...

  9. PAT A 1004. Counting Leaves (30)【vector+dfs】

    题目链接:https://www.patest.cn/contests/pat-a-practise/1004 大意:输出按层次输出每层无孩子结点的个数 思路:vector存储结点,dfs遍历 #in ...

  10. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

随机推荐

  1. jave 金额科学记数法处理

    金额 :amount amount.stripTrailingZeros().toPlainString();

  2. Html5的placeholder属性(IE兼容)

    HTML5对Web Form做了很多增强,比方input新增的type类型.Form Validation等. Placeholder是HTML5新增的还有一个属性,当input或者textarea设 ...

  3. iOS开发之──传感器使用

    本文转载至 http://mobile.51cto.com/iphone-423219.htm 在实际的应用开发中,会用到传感器,下面首先介绍一下iphone4的传感器,然后对一些传感器的开发的API ...

  4. wince c# 创建桌面快捷方式 自动启动 只运行一次 全屏显示

    using System; using System.Linq; using System.Collections.Generic; using System.Text; using System.R ...

  5. java CyclicBarrier和wait/notifyAll

    1 CyclicBarrier 多个进程做自己的事情,然后先做完的就等待在CyclicBarrier上,然后最后一个做完的线程到来时会冲破CyclicBarrier,然后执行CyclicBarrier ...

  6. Building REST services with Spring

    https://spring.io/guides/tutorials/bookmarks/

  7. MFC HTTP(S)请求笔记

    GET示例 #include <afxinet.h> #include <iostream> #include <vector> #ifdef _UNICODE # ...

  8. Spring 实战 学习笔记(1)

    Spring的核心 依赖注入 & 切面编程 1.创建应用组件之间协作的行为通常称为装配.(wiring) 2.Spring EL表达式.SEL是一种能在运行时构建复杂表达式,存取对象属性.对象 ...

  9. php mcrypt加密实例

    <?php //当前mcrypt支持的加密模型 $modes_list = mcrypt_list_modes(); // Array // ( // [0] => cbc // [1] ...

  10. 常见的LINUX发行版安装libiconv库方法

    今天编译程序,发现程序报错,如下 cannot find -liconv collect2: ld returned 1 exit status 或者 undefined reference to ` ...