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:

  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

题意:

给定一棵树和节点之间的关系。要求统计每一层的叶子节点个数。

思路:

就建树,暴力dfs就好了。maxn=105的时候WA和RE了,改成了1005就过了。

  1. #include <iostream>
  2. #include <set>
  3. #include <cmath>
  4. #include <stdio.h>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #include <map>
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. typedef long long LL;
  13. #define inf 0x7f7f7f7f
  14.  
  15. const int maxn = ;
  16. int n, m;
  17. struct node{
  18. int v, nxt;
  19. }edge[maxn];
  20. int head[maxn], tot = ;
  21. int cnt[maxn], dep = -;
  22.  
  23. void addedge(int u, int v)
  24. {
  25. edge[tot].v = v;
  26. edge[tot].nxt = head[u];
  27. head[u] = tot++;
  28. edge[tot].v = u;
  29. edge[tot].nxt = head[v];
  30. head[v] = tot++;
  31. }
  32.  
  33. void dfs(int rt, int fa, int h)
  34. {
  35. int sum = ;
  36. dep = max(dep, h);
  37. for(int i = head[rt]; i != -; i = edge[i].nxt){
  38. if(edge[i].v == fa)continue;
  39. sum++;
  40. dfs(edge[i].v, rt, h + );
  41. }
  42. if(sum == ){
  43. cnt[h]++;
  44. }
  45.  
  46. }
  47.  
  48. int main()
  49. {
  50. scanf("%d%d", &n, &m);
  51. memset(head, -, sizeof(head));
  52. for(int i = ; i < m; i++){
  53. int u, k;
  54. scanf("%d %d", &u, &k);
  55. for(int j = ; j < k; j++){
  56. int v;
  57. scanf("%d", &v);
  58. addedge(u, v);
  59. }
  60. }
  61.  
  62. dfs(, -, );
  63. //cout<<dep<<endl;
  64. printf("%d", cnt[]);
  65. for(int i = ; i <= dep; i++){
  66. printf(" %d", cnt[i]);
  67. }
  68. printf("\n");
  69. return ;
  70. }

PAT甲1004 Counting Leaves【dfs】的更多相关文章

  1. PAT Advanced 1004 Counting Leaves

    题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...

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

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

  3. PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]

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

  4. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  5. PAT甲级 1004.Counting Leaves

    参考:https://blog.csdn.net/qq278672818/article/details/54915636 首先贴上我一开始的部分正确代码: #include<bits/stdc ...

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

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

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

  8. PAT 1004 Counting Leaves (30分)

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

  9. 1004 Counting Leaves (30分) DFS

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

随机推荐

  1. selenium chrome登陆手机 pc淘宝

    接口登录淘宝,困难度极高,没有人已经实现过. 淘宝登录selenium 手机版  pc版. 由于每天需要使用ip代理大批量的异地登录淘宝帐号,这种情况必然会出现淘宝滑动验证码,使用ActionChai ...

  2. MTK 时区修改

    1.修改packages/apps/Settings/res/xml-xx-xx/timezones.xml (xx-xx表示不同的语言和区域),添加下面的内容:     <!-- timezo ...

  3. 为什么要使用JS模板引擎

    我之前在写一个输入联想控件的时候,改过好几个版本,每个版本不是因为性能不好就是因为代码凌乱而被推翻,最后用了understore模板引擎,效果有明显改善.整好这两天在研究互联网技术架构,发现很多的开发 ...

  4. 架构设计:系统存储(28)——分布式文件系统Ceph(挂载)

    (接上文<架构设计:系统存储(27)--分布式文件系统Ceph(安装)>) 3. 连接到Ceph系统 3-1. 连接客户端 完毕Ceph文件系统的创建过程后.就能够让客户端连接过去. Ce ...

  5. C mysql (C API Commands out of sync; you can't run this command now)

    错误出现在当一个用户使用查询,另一个用户再使用此sql连接进行查询的时候: 原因是因为上一次使用此sql连接进行查询时没有将所有的结果集给释放掉,在所有使用此sql连接进行查询的地方将所有的结果集给释 ...

  6. centos7修改root密码

    1.重启系统,在下面界面时按e键 2.出现可编辑新内容,按向下键向下滑动,找到ro,并修改为rw 后,在LANG=en_US.UTF-8后面再加init=/bin/sh,结果如下图 3.然后按下ctr ...

  7. MongoDB(三)-- 执行JS、界面工具

    一.执行Js脚本 1.开启mongod服务 2.连接mongodb客户端,./mongo --host 192.168.80.128 --port 27017 3.创建数据库:use testdb1 ...

  8. Splash 简介与安装

    Splash 说白了就是一个轻量级的浏览器,利用它,我们同样可以实现跟其他浏览器一样的操作,我们使用 Docker 来安装 Splash: [root@localhost ~]# docker run ...

  9. ListView实现多种item布局的方法和注意事项

    这篇文章的效果也是大家常见的,各种通讯应用的对话列表都是这种方式,像微信.whatsapp.易信.米聊等.我们这篇文章也权当为回忆,形成简单的笔记.这篇文章参考了2009年Google IO中的< ...

  10. WIN32编程经验总结

    一 窗口和消息 1. 前缀: 2 WPARAM和LPARAM的意义在Windows是一种16位系统时,WndProc的第三个参数被定义为WORD,是一个16位的无符号整数,而第四个参数被定义为一个LO ...