zoukankan      html  css  js  c++  java
  • 1004. Counting Leaves (30)

    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

    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:

    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

    2 1
    01 1 02
    
    

    Sample Output

    0 1
    

    题意:给出一棵树,问每一层各有多少个叶子结点。

    分析: bfs解决即可

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    #include <string>
    using namespace std;
    
    struct Node{
      int level;
      int firstChild;
      int nextSbiling;
      int childNum;
    };
    
    Node node[101];
    void BFS(){
    
      queue<int> level;
      int NodeTobelevel[101];
      memset(NodeTobelevel,0, sizeof(NodeTobelevel));
    
      node[1].level = 0;
      level.push(1);
      int curId, tmpId,curlevel = 0;
      while(!level.empty()){
    
        curId = level.front();
        level.pop();
        int num = node[curId].childNum;
        curlevel = node[curId].level;
    
        if (num==0)
        {
          NodeTobelevel[node[curId].level]++;
          continue;
        }
    
        tmpId = node[curId].firstChild;
            node[tmpId].level = node[curId].level+1;
            level.push(tmpId);
            num--;
    
    
        while(num--){
          tmpId = node[tmpId].nextSbiling;
          node[tmpId].level = node[curId].level+1;
          level.push(tmpId);
        }
      }
      for (int i = 0; i <= curlevel; ++i){
        if (i==0){
          printf("%d", NodeTobelevel[i]);
        }else{
          printf(" %d", NodeTobelevel[i]);
        }
      }
    }
    
    int main(){
      int N,M;
      scanf("%d%d", &N,&M);
      int curId, firstSibling;
    
      memset(node,0, sizeof(node));
    
      //input node
      for (int i = 0; i < M; ++i)
      {
        scanf("%d",&curId);
        scanf("%d",&(node[curId].childNum));
        scanf("%d",&(node[curId].firstChild));
    
        firstSibling = node[curId].firstChild;
        for (int i = 0; i < node[curId].childNum-1; ++i)
        {
          scanf("%d",&(node[firstSibling].nextSbiling));
          firstSibling = node[firstSibling].nextSbiling;
        }
      }
      BFS();
    }
    
  • 相关阅读:
    python查询MySQL写入Excel
    Spring BOOT的学习笔记
    后台管理系统好用的UI框架
    SSM学习笔记
    解决thinkPHP3.2.3使用Smarty模板后无法使用系统常量问题
    提交代码,SVN被锁定,提示:svn is already locked解决方案
    CentOS 7编译安装php7.0.7以及可能遇到的问题的解决方案
    thinkphp nginx配置
    php读取不到指定的php.ini配置
    phpmyadmin nginx设置
  • 原文地址:https://www.cnblogs.com/Dyleaf/p/8725810.html
Copyright © 2011-2022 走看看