• 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 <string>
     4 #include <vector>
     5 #include <queue>
     6 #include <iostream>
     7 using namespace std;
     8 struct node{
     9     int floor,sonnum;
    10     vector<int> v;
    11     node(){
    12         sonnum=0;
    13     }
    14 };
    15 node t[105];
    16 int f[105];
    17 int main(){
    18     //freopen("D:\INPUT.txt","r",stdin);
    19     int n,m;
    20     int i,num,j,k,maxfloor;
    21     memset(f,0,sizeof(f));
    22     scanf("%d %d",&n,&m);
    23 
    24     //cout<<n<<" "<<m<<endl;
    25 
    26     for(i=0;i<m;i++){
    27         scanf("%d",&num);
    28 
    29         //cout<<num<<endl;
    30 
    31         t[num].floor=0;
    32         scanf("%d",&t[num].sonnum);
    33 
    34         //cout<<t[num].sonnum<<endl;
    35 
    36         for(j=0;j<t[num].sonnum;j++){
    37             scanf("%d",&k);
    38             t[num].v.push_back(k);
    39         }
    40     }
    41     queue<int> q;
    42     int top=1;
    43     maxfloor=0;
    44     q.push(top);
    45     while(!q.empty()){
    46         top=q.front();
    47         q.pop();
    48         if(!t[top].sonnum){
    49             f[t[top].floor]++;
    50             continue;
    51         }
    52 
    53         //cout<<t[top].floor<<endl;
    54 
    55         if(t[top].floor+1>maxfloor){
    56             maxfloor=t[top].floor+1;
    57             //cout<<maxfloor<<endl;
    58         }
    59         for(i=0;i<t[top].sonnum;i++){
    60             t[t[top].v[i]].floor=t[top].floor+1;
    61             q.push(t[top].v[i]);
    62         }
    63     }
    64 
    65     //cout<<maxfloor<<endl;
    66     printf("%d",f[0]);
    67     for(i=1;i<=maxfloor;i++){
    68         printf(" %d",f[i]);
    69     }
    70     printf("
    ");
    71     return 0;
    72 }
  • 相关阅读:
    C#将一个字符串数组的元素的顺序进行反转
    C#找出100内所有的素数/质数
    C#流程控制for循环语句,水仙花数。
    C# 常用的操作文件夹的方法
    Element UI
    JS
    JS
    PHP基础算法
    js实现csv下载
    el-dialog“闪动”解决办法
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4661806.html
Copyright © 2020-2023  润新知