• PAT 1004 Counting Leaves


    1004 Counting Leaves

    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

    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:

    0 1

    救命, 不会写代码呀!!!

    还是看看大佬的代码吧

    题意:这题就是要你求出每一层中叶节点的个数

    数据结构:

            (1)利用 vector二维数组 来存储每个节点的子节点,有点类似于邻接表,好处是可以直接使用 stl 自带的函数 .size() 直接计算出数组的大小,从而判断该节点是否为叶节点

            (2)数组 book 存放每一层叶节点的个数

    思路:dfs 算法遍历每一个,可以在函数参数中假如变量表示当前所在的层数,假如当前点是叶节点,就在该层加一表示在该层发现了一个新的叶节点

    dfs:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. vector<int> v[100];
    6. int book[100], maxdepth = -1;
    7. void dfs(int index, int depth) {
    8. if(v[index].size() == 0) {
    9. book[depth]++;
    10. maxdepth = max(maxdepth, depth);
    11. return ;
    12. }
    13. for(int i = 0; i < v[index].size(); i++)
    14. dfs(v[index][i], depth + 1);
    15. }
    16. int main() {
    17. int n, m, k, node, c;
    18. scanf("%d %d", &n, &m);
    19. for(int i = 0; i < m; i++) {
    20. scanf("%d %d",&node, &k);
    21. for(int j = 0; j < k; j++) {
    22. scanf("%d", &c);
    23. v[node].push_back(c);
    24. }
    25. }
    26. dfs(1, 0);//因为
    27. printf("%d", book[0]);
    28. for(int i = 1; i <= maxdepth; i++)
    29. printf(" %d", book[i]);
    30. return 0;
    31. }

    bfs:在求第几层的地方实质上就是求当前点到根节点的路劲(假设每个子节点和父节点的距离都为1)

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. const int N=110;
    6. int book[N],level[N];
    7. int n,m,M=-1;
    8. vector<int> v[N];
    9. void bfs(){
    10. queue<int> q;
    11. q.push(1);
    12. level[1]=0;
    13. while(q.size()){
    14. int t=q.front();
    15. q.pop();
    16. if(v[t].size()==0){
    17. M=max(M,level[t]);
    18. book[level[t]]++;
    19. }
    20. for(int i=0;isize();i++){
    21. q.push(v[t][i]);
    22. level[v[t][i]]=level[t]+1;
    23. }
    24. }
    25. }
    26. int main(){
    27. cin >> n >> m;
    28. int k,node,r;
    29. for(int i=0;i
    30. scanf("%d%d",&node,&k);
    31. for(int j=0;j
    32. scanf("%d",&r);
    33. v[node].push_back(r);
    34. }
    35. }
    36. bfs();
    37. printf("%d",book[0]);
    38. for(int i=1;i<=M;i++) printf(" %d",book[i]);
    39. return 0;
    40. }

    好好学习,天天向上!

    我要考研!    

  • 相关阅读:
    9月16日,每日信息差
    【Detectron2】代码库学习-5.标注格式- 矩形框, 旋转框,关键点, mask, 实例标注,IOU计算, 旋转框IOU计算,
    没有可用软件包 docker-ce。 错误:无须任何处理
    使用IDEA创建一个SpringBoot项目
    Pandas光速入门-一文掌握数据操作
    MyBatis 源码分析--获取SqlSession
    windows 11+docker desktop+grafana+influxDB
    为什么说企业需要数据可视化报表,浅谈数据可视化报表的真正价值
    5种常见的软件缺陷分析方法
    【发表案例】智能物联网类SCI&EI,仅25天录用,计算机领域必投SCI快刊,12月截稿
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/126592968