1004 Counting Leaves (30 point(s))

1004 Counting Leaves (30 point(s))

A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.

Input Specification:

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.

The input ends with N being 0. That case must NOT be processed.

Output Specification:

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.

题目大意:

给定对应的树的结构.然后给出每一层的叶子节点的数量.

解决:

1.首先是输入的转化,要将01 ...02 等等数字转化成正常的数字,解决方案就是使用sstream  通过string转化为int

2.其次是树的存储和构造,这里用了vector数组进行存储,因为知道范围,所以直接创建是很方便的

3.最后是树的层序遍历,使用queue进行层序遍历每一层遍历完之后,直接打印对应的叶子节点的数量

4.注意最后的打印格式

#include<iostream>
#include<vector>
#include<sstream>
#include<string>
#include<queue>
using namespace std;


int strToint(const string &str){
    istringstream i(str);
    int out;
    if(i>>out){
        return out;
    }
    return 0;
}

int main(){
    int const MAX = 110;
    bool vis[MAX]{false};
    int father[MAX];
    vector<int>tree[MAX];
    queue<int>finder;

    fill(father,father+MAX,1);
    fill(vis,vis+MAX,0);

    int N=0;
    int M=0;

    cin>>N>>M;

    if(N==0){
        return 0;
    }

    for(int i=0;i<M;i++){
        string temp;
        int num;
        cin>>temp>>num;
        int id = strToint(temp);
        vis[id] =true;
        for(int j=0;j<num;j++){
            string temp_num;
            cin>>temp_num;
            int temp_id = strToint(temp_num);
            vis[temp_id] = true;
            tree[id].push_back(temp_id);
            father[temp_id] = id;
        }
    }
    finder.push(1);
    int Level =1;
    int oldlevel =1;
    int Count=0;
    bool first=true;
    while(!finder.empty()){
        int id = finder.front();
        finder.pop();
        int lenth = tree[id].size();
        if(lenth==0){
            Count++;
        }
        for(int i=0;i<lenth;i++){
            finder.push(tree[id][i]);
            Level = tree[id][i];
        }

        if(id==oldlevel){
            oldlevel = Level;
            if(first){
                cout<<Count;
                first = false;
            }else{
                cout<<" "<<Count;
            }
            Count=0;
        }

    }

    return 0;

}

一开始理解错了,以为是要找到叶子节点并且最后打印从root到该叶子节点的路径...审题很总要

生僻单词:

hierarchy 

n. 层级;等级制度

pedigree

  • n. 血统;家谱
  • adj. 纯种的

specification

n. 规格;说明书;详述

sake

  • n. 目的;利益;理由;日本米酒
  • n. (Sake)人名;(罗)萨克;(日)酒(姓)

seniority

n. 长辈;老资格;前任者的特权

猜你喜欢

转载自blog.csdn.net/Willen_/article/details/83963498
今日推荐