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();
    }
    
  • 相关阅读:
    js中let和var定义变量的区别
    windows下开发PHP扩展dll(无需Cygwin)
    用VS开发PHP扩展
    破解电信光猫华为HG8120C关闭路由功能方法
    从程序员到项目经理(二十九):怎样写文档
    从程序员到项目经理(二十八):该死的结果导向(只看结果,不问过程到底行不行?)
    从程序员到项目经理(二十七):怎样给领导汇报工作
    从程序员到项目经理(二十六):项目管理不能浑水摸鱼
    从程序员到项目经理(二十五):对绩效考核的吐槽
    从程序员到项目经理(二十四):慎于问敏于行
  • 原文地址:https://www.cnblogs.com/Dyleaf/p/8725810.html
Copyright © 2011-2022 走看看