zoukankan      html  css  js  c++  java
  • PAT 1004 Counting Leaves

    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.

    Sample Input:

    2 1
    01 1 02

    Sample Output:
    0 1

    题目读起来比较拗口,N节点的数量,M非叶子节点的数量,后面会有M行输入,每行输入内容是,“父节点 子节点数量,子节点序列”

    #include <iostream>
    #include <list>
    using namespace std;
    int main(){
        int nodeCount;
        int nonLeafCount;
        //输入n m
        cin >> nodeCount >> nonLeafCount;
        list<int> *adj = new list<int>[100];
        for (int i = 0; i < nonLeafCount; ++i) {
            int parent;
            int k;
            cin >> parent >> k;
            for (int j = 0; j < k; ++j) {
                int child;
                cin >> child;
        list<int> nodeList;
        list<int> resultList;
            if (nodeList.empty()){
            int leafCount = 0;
            list<int> nextLevelNodeList;
            while(true) {
                if (nodeList.empty()){
                int node = nodeList.front();
                    list<int>::iterator it;
                    for(it= adj[node].begin(); it != adj[node].end(); it++){
                } else {
            nodeList = nextLevelNodeList;
            auto it = resultList.begin();
            cout << *it;
            for(; it != resultList.end(); it++){
                cout << " " << *it;
  • 相关阅读:
    DSP28335 烧写到RAM和FLASH方法
    基于飞秒光频梳的正弦相位调制干涉绝对距离测量方法研究 -张世华
    DSP28335 IO口寄存器
    DSP28335 使用技巧
    freertos、UCos这种实时操作系统和Linux、Windows这种系统的 本质区别
    stm32的定时器输入捕获 与输出比较
  • 原文地址:https://www.cnblogs.com/IC1101/p/13621060.html
Copyright © 2011-2022 走看看