zoukankan      html  css  js  c++  java
  • 【PAT甲级】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

    题目大意:求出树的每一层叶子结点的个数
    考察点:图的遍历
    坑点:要求出树的深度,按要求输出
    解决方案:广度优先遍历
     1 #include <iostream>
     2 #include <queue>
     3 #include <cstring> 
     4 #define N 105
     5 using namespace std;
     6 //存储树 
     7 bool a[N][N];
     8 //标记某节点是否为叶子结点 
     9 bool isLeaf[N];
    10 //记录树的每一层叶子结点的个数 
    11 int level[N];
    12 //定义节点类型的结构体
    13 //index: 节点编号
    14 //level: 所在层编号 
    15 struct node{
    16     int index,level;
    17     node(int i,int l){
    18         index=i;
    19         level=l;
    20     }
    21 };
    22 int main(){
    23     memset(level,0,sizeof(level));
    24     memset(a,false,sizeof(a));
    25     memset(isLeaf,true,sizeof(isLeaf));
    26     int n,m;
    27     cin>>n>>m;
    28     int id,k,val;
    29     for(int i=0;i<m;i++){
    30         cin>>id>>k;
    31         isLeaf[id]=false;
    32         for(int j=0;j<k;j++){
    33             cin>>val;
    34             a[id][val]=true;
    35         }
    36     }
    37     
    38     
    39     queue<node> bfs;
    40     bfs.push(node(1,1));
    41     //记录树的深度 
    42     int maxlevel=1;
    43     //开始广度优先遍历 
    44     while(!bfs.empty()){
    45         node temp=bfs.front();
    46         bfs.pop();
    47         int k=temp.index;
    48         maxlevel=temp.level;
    49         if(isLeaf[k])
    50             level[temp.level]++;
    51         for(int i=1;i<=n;i++)
    52             if(a[k][i]==true)
    53                 bfs.push(node(i,temp.level+1));
    54     }
    55     for(int i=1;i<maxlevel;i++)
    56         cout<<level[i]<<" ";
    57     cout<<level[maxlevel]<<endl;
    58     return 0;
    59 } 
  • 相关阅读:
    夺命雷公狗TP3.2.3商城11-----后台头部和左侧导航分离
    夺命雷公狗TP3.2.3商城10-----中间层的创建,防止跨目录访问
    3.spring cloud eureka 高可用
    2.spring cloud eureka client配置
    1.spring cloud eureka server配置
    css去掉input记住密码的黄色
    34-THREE.JS 游泳圈
    32-THREE.JS 球体
    31-THREE.JS 正方体
    30-THREE.JS 圆环
  • 原文地址:https://www.cnblogs.com/vmoor2016/p/6742185.html
Copyright © 2011-2022 走看看