1004. Counting Leaves (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input

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.

Output

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

提交代码

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <string>
  4. #include <vector>
  5. #include <queue>
  6. #include <iostream>
  7. using namespace std;
  8. struct node{
  9. int floor,sonnum;
  10. vector<int> v;
  11. node(){
  12. sonnum=;
  13. }
  14. };
  15. node t[];
  16. int f[];
  17. int main(){
  18. //freopen("D:\\INPUT.txt","r",stdin);
  19. int n,m;
  20. int i,num,j,k,maxfloor;
  21. memset(f,,sizeof(f));
  22. scanf("%d %d",&n,&m);
  23.  
  24. //cout<<n<<" "<<m<<endl;
  25.  
  26. for(i=;i<m;i++){
  27. scanf("%d",&num);
  28.  
  29. //cout<<num<<endl;
  30.  
  31. t[num].floor=;
  32. scanf("%d",&t[num].sonnum);
  33.  
  34. //cout<<t[num].sonnum<<endl;
  35.  
  36. for(j=;j<t[num].sonnum;j++){
  37. scanf("%d",&k);
  38. t[num].v.push_back(k);
  39. }
  40. }
  41. queue<int> q;
  42. int top=;
  43. maxfloor=;
  44. q.push(top);
  45. while(!q.empty()){
  46. top=q.front();
  47. q.pop();
  48. if(!t[top].sonnum){
  49. f[t[top].floor]++;
  50. continue;
  51. }
  52.  
  53. //cout<<t[top].floor<<endl;
  54.  
  55. if(t[top].floor+>maxfloor){
  56. maxfloor=t[top].floor+;
  57. //cout<<maxfloor<<endl;
  58. }
  59. for(i=;i<t[top].sonnum;i++){
  60. t[t[top].v[i]].floor=t[top].floor+;
  61. q.push(t[top].v[i]);
  62. }
  63. }
  64.  
  65. //cout<<maxfloor<<endl;
  66. printf("%d",f[]);
  67. for(i=;i<=maxfloor;i++){
  68. printf(" %d",f[i]);
  69. }
  70. printf("\n");
  71. return ;
  72. }

pat1004. Counting Leaves (30)的更多相关文章

  1. 1004. Counting Leaves (30)

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

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

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

  3. PAT1004:Counting Leaves

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

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

  5. PAT 1004 Counting Leaves (30分)

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

  6. 1004 Counting Leaves (30分) DFS

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

  7. PAT-1004 Counting Leaves

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

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

随机推荐

  1. js定时执行函数

    //方法一: //直接现定义函数 var time = window.setInterval(function(){ $('.lingdao_right').click(); },5000); //方 ...

  2. java的多线程安全,ReentrantLock与synchronized锁

    前言 多线程总的来说是一个很大的模块,所以虽然之前就想写但一直感觉有地方没有理解透,在经过了一段时间学习后,终于有点感觉了,在此写下随笔. 多线程安全问题##: 首先和大家讨论一下多线程为什么会不安全 ...

  3. leecode刷题(2)-- 买卖股票的最佳时机

    买卖股票的最佳时机 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多 ...

  4. DHCP与PPPOE 区别

    1.静态IP的方式,如果是占用一个INTERNET的IP的话,上网都是很贵的,当然这个也是最方便的,开机就能上网,不用做任何拨号或者认证的过程.2.PPPOE,只是多了一个获得IP的过程,一旦获得了I ...

  5. Win7下C/C++跨平台开发工具IDE的安装之CodeBlocks

    1. Win7下安装CodeBlocks: 下载带有mingw的CodeBlocks:http://www.codeblocks.org/downloads/26#windows 运行所下载程序: 点 ...

  6. WPF强制设置TextBox文本框的焦点

    在需求中遇到这样一种场景:就是在无论何时都要把焦点设置在一个TextBox中. 引用空间:System.Windows.Input 方式1:在窗体的Load事件中去设置焦点,(注意:不能在窗体的构造函 ...

  7. ARKit的使用

    //创建场景 let scene = SCNScene() /* //1.几何 let box = SCNBox.init(width: 0.1, height: 0.1, length: 0.1, ...

  8. 关于zabbix _get返回Could not attach to pid的问题

    分析:这是因为由于zabbix客户端没有权限,导致zabbix_server端无法获取到键值数据!! 解决:在客户端上添加用户sudo的权限就可以解决了. 验证:是脚本的话还要在脚本里加上sudo的命 ...

  9. JavaWeb学习笔记(六)—— Cookie&Session

    一.会话技术简介 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 在日常生活中,从拨通电话到挂断电话之间的一连串的你问我答的过程 ...

  10. 在pycharm中使用指定的虚拟环境virtualenv

    第一步 建立虚拟环境 cmd下: pip install virtualenv 创建虚拟环境目录 env 第二步:激活虚拟环境 进入虚拟环境下的script目录 第三步:创建一个flask文件夹并打开 ...