• pat1004. Counting Leaves (30)


    1004. Counting Leaves (30)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue
    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<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<stack>
     5 #include<set>
     6 #include<map>
     7 #include<queue>
     8 #include<algorithm>
     9 using namespace std;
    10 vector<int> node[105];
    11 int sum[105];
    12 int main(){
    13     //freopen("D:\INPUT.txt","r",stdin);
    14     int n,m;
    15     scanf("%d %d",&n,&m);
    16     int i,j,num,first,v;
    17     for(i=1;i<=m;i++){
    18         scanf("%d %d",&first,&num);
    19         while(num--){
    20             scanf("%d",&v);
    21             node[first].push_back(v);
    22         }
    23     }
    24     int last,e,cur,level=1;
    25     queue<int> q;
    26     if(node[1].size()){
    27         last=e=1;
    28         q.push(1);
    29         level++;
    30     }
    31     else{
    32         sum[level]++;
    33     }
    34 
    35     //cout<<level<<endl;
    36 
    37     while(!q.empty()){
    38         cur=q.front();
    39 
    40         //cout<<cur<<endl;
    41 
    42         q.pop();
    43         for(j=0;j<node[cur].size();j++){
    44             if(node[node[cur][j]].size()){
    45                 q.push(node[cur][j]);
    46                 last=node[cur][j];
    47             }
    48             else{
    49                 sum[level]++;
    50             }
    51         }
    52         if(e==cur){
    53             e=last;
    54             level++;//指向下一层
    55 
    56             //cout<<level<<endl;
    57 
    58         }
    59     }
    60 
    61     //cout<<level<<endl;
    62 
    63     printf("%d",sum[1]);
    64     for(i=2;i<level;i++){
    65         printf(" %d",sum[i]);
    66     }
    67     printf("
    ");
    68     return 0;
    69 }
  • 相关阅读:
    c语言实现BMP图像转换为灰度图
    360初赛溢出题
    vim的完全卸载
    小谈截断上传漏洞
    cmd提权的一些常用命令
    渗透测试方向概览
    字符编码以及python的编码解释
    BrainFuck 以及运用(idf)
    记一次加解密通关Nazo
    360 心情杂记
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4802201.html
Copyright © 2020-2023  润新知