zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    ffmpeg命令行获取RTSP流并每秒截取一张解码存储为jpg
    快速排序
    ffmpeg参数中文详细解释
    Docker
    elasticsearch的安装和配置
    C 语言-运算符(算术运算符,类型转换,赋值运算符,自增,自减,sizeof 运算符,逗号运算符,关系运算符,逻辑运算符,三目运算符)
    c语言-printf函数和scanf函数简单使用
    将JPA通用的实现方法封装成一个抽象类进行拓展
    工具类里的静态变量如何获取application.yml或者application.properties里的属性
    记java实体类属性名为全部为大写踩的坑(基础)
  • 原文地址:https://www.cnblogs.com/Deribs4/p/4661806.html
Copyright © 2011-2022 走看看