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:

  1. 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:

  1. 2 1
  2. 01 1 02

Sample Output:

  1. 0 1
  2. DFS:因为要知道有多少层,每次进DFS先更新一下最大层数
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,k,id,m,k2;
  4. vector<int> v[];
  5. int num[];
  6. int maxn = ;
  7. void find(int a,int b)
  8. {
  9. maxn = max(maxn,b);
  10. if(v[a].size() == ){
  11. num[b] += ;
  12. return ;
  13. }
  14. else{
  15. for(int i=;i<v[a].size();i++){
  16. find(v[a][i],b+);
  17. }
  18. }
  19. }
  20. int main()
  21. {
  22. scanf("%d %d",&n,&m);
  23. memset(num,,sizeof(num));
  24. for(int i=;i<=m;i++)
  25. {
  26. scanf("%d",&id);
  27. scanf("%d",&k);
  28. for(int j=;j<k;j++)
  29. {
  30. scanf("%d",&k2);
  31. v[id].push_back(k2);
  32. }
  33. }
  34. find(,);
  35. for(int i=;i<=maxn;i++)
  36. {
  37. if(i==){
  38. printf("%d",num[i]);
  39. }
  40. else{
  41. printf(" %d",num[i]);
  42. }
  43. }
  44. return ;
  45. }

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. Spring Boot + Spring Data JPA + PostgreSQL

    最近在用Java重写之前实习生用.netcore写的微信后台应用. 规定用Spring Boot框架,PostgreSQL数据库.之前一直习惯于基于XML的Spring app,也没用过Postgre ...

  2. 图像处理之opencv---加减乘除运算cvdiv

    http://chyyeng.blog.163.com/blog/static/16918230201211632135456/

  3. android lanchmode

    http://www.cnblogs.com/xiaoQLu/archive/2012/07/17/2595294.html http://www.cnblogs.com/lwbqqyumidi/p/ ...

  4. SQL 游标示例

    DECLARE @i INT ) --给初始值 CREATE TABLE #temp_test --创建临时表 ( num ) ) ) BEGIN INSERT INTO #temp_test ( n ...

  5. 使用 QWorker 做为计划任务引擎

    QWorker 提供了 Plan 函数来提供计划任务功能的支持.每个任务做为一个作业,可以在指定的时间点被触发执行.而 cron 作为 Linux 操作系统下计划任务的标准被广大用户所熟知,QWork ...

  6. C#实现(递归和非递归)高速排序和简单排序等一系列排序算法

        本人由于近期工作用到了一些排序算法.就把几个简单的排序算法.想冒泡排序,选择排序,插入排序.奇偶排序和高速排序等整理了出来,代码用C#代码实现,而且通过了測试.希望能给大家提供參考.     ...

  7. machine learning for hacker记录(1) R与机器学习

    开篇:首先这本书的名字很霸气,全书内容讲的是R语言在机器学习上面的应用,一些基本的分类算法(tree,SVM,NB),回归算法,智能优化算法,维度约减等,机器学习领域已经有很多成熟的R工具箱,毕竟这个 ...

  8. SQL 系统表应用

    查看link server select DD.System,DD.DB,DD.previous_processing_dte, DD.processing_dte,LS.LinkServerName ...

  9. Centos7利用kvm搭建Windows虚拟机

    这几天玩了一下kvm虚拟化,真的很有意思,我把这几天踩的坑,还有收获,都记录下来,作为以后的复习和检查. 首先说一下我的基本逻辑,我有一台win7的笔记本,我的底层虚拟化是使用VMWare构建的Cen ...

  10. Gym - 101147G G - The Galactic Olympics —— 组合数学 - 第二类斯特林数

    题目链接:http://codeforces.com/gym/101147/problem/G G. The Galactic Olympics time limit per test 2.0 s m ...