problem

  1. A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
  2. Input
  3. 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:
  4. ID K ID[1] ID[2] ... ID[K]
  5. 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.
  6. Output
  7. 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.
  8. 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.
  9. Sample Input
  10. 2 1
  11. 01 1 02
  12. Sample Output
  13. 0 1

tip

一颗树,找到每层的叶子数量。

anwser

  1. #include<bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3. #define Max 101
  4. #define mp(x, y) std::make_pair(x, y)
  5. int N, M, level[Max], maxLevel = 0;
  6. std::vector<int > kids[Max];
  7. //typedef std::pair<int ,int> node
  8. int main(){
  9. // freopen("test.txt", "r", stdin);
  10. memset(level, 0, sizeof(level));
  11. std::cin>>N>>M;
  12. int fa, kidNum, kid;
  13. for(int i = 0; i < M; i++){
  14. std::cin>>fa>>kidNum;
  15. for(int j = 0; j < kidNum; j++){
  16. std::cin>>kid;
  17. kids[fa].push_back(kid);
  18. // std::cout<<kid<<" ";
  19. }
  20. // std::cout<<std::endl;
  21. }
  22. // for(int i = 1; i <= N; i++) {
  23. // if(i == 1)std::cout<< kids[i].size();
  24. // else std::cout<<" "<<kids[i].size();
  25. // }
  26. std::queue<std::pair<int, int> > que;
  27. que.push(mp(1,0));
  28. while(!que.empty()){
  29. std::pair<int, int> father = que.front(); que.pop();
  30. if(kids[father.first].size() == 0) {
  31. // std::cout<<father.first<<std::endl;
  32. level[father.second] ++;
  33. maxLevel = father.second;
  34. continue;
  35. }
  36. for(int i = 0; i < kids[father.first].size(); i++){
  37. que.push(mp(kids[father.first][i], father.second+1));
  38. }
  39. }
  40. for(int i = 0; i <= maxLevel; i++)
  41. {
  42. if (i == 0) std::cout<<level[i];
  43. else std::cout<<" "<<level[i];
  44. }
  45. return 0;
  46. }
  47. /*
  48. 4 2
  49. 01 1 02
  50. 02 2 03 04
  51. */

experience

英语单词:

  • hierarchy 层级关系

1004 Counting Leaves (30)(30 point(s))的更多相关文章

  1. PAT Advanced 1004 Counting Leaves

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

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

  3. 1004. Counting Leaves (30)

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

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

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

  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【dfs】

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

  8. A1095 Cars on Campus (30)(30 分)

    A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...

  9. 1004 Counting Leaves ——PAT甲级真题

    1004 Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to coun ...

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

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

随机推荐

  1. bzoj 5055: 膜法师——树状数组

    Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...

  2. 【转】C#使用PrintDocument打印 多页 打印预览

    PrintDocument实例所有的订阅事件如下: 创建一个PrintDocument的实例.如下: System.Drawing.Printing.PrintDocument docToPrint ...

  3. MongoDB警告信息

    更多内容推荐微信公众号,欢迎关注: MongoDB警告信息: 1. WARNING: Using the XFS filesystem is strongly recommended with the ...

  4. 最小生成树 kuangbin专题最后一个题

    题目链接:https://cn.vjudge.net/contest/66965#problem/N 注释:这道题需要用krustra,用prim的话可能会超时.并且在计算距离的时候要尽量减少步骤,具 ...

  5. Strusts2笔记6--拦截器

    拦截器: Struts2的大多数核心功能都是通过拦截器实现的.拦截器之所以称之为“拦截器”,是因为它可以在执行Action之前或之后拦截下用户请求,执行一些操作,以增强Action方法的功能. Str ...

  6. clog,cout,cerr 输出机制

    clog:控制输出,使其输出到一个缓冲区,这个缓冲区关联着定义在 <cstdio> 的 stderr. cerr:强制输出刷新,没有缓冲区. cout:控制输出,使其输出到一个缓冲区,这个 ...

  7. HTML5 Differences from HTML4

    Abstract "HTML5 Differences from HTML4" describes the differences of the HTML5 specificati ...

  8. Javascript之浏览器兼容EventUtil

    var EventUtil = { //添加事件处理程序 addHandler: function (element, type, handler) { if (element.addEventLis ...

  9. Java标记接口

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------这篇博客主要来谈谈" ...

  10. 使用Appium 测试微信小程序和微信公众号方法

    由于腾讯系QQ.微信等都是基于腾讯自研X5内核,不是google原生webview,需要打开TBS内核Inspector调试功能才能用Chrome浏览器查看页面元素,并实现Appium自动化测试微信小 ...