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(){
    //节点数n
    int nodeCount;
    //非叶子节点数m
    int nonLeafCount;
    //输入n m
    cin >> nodeCount >> nonLeafCount;
    //邻接表(index为父节点,list位子节点列表,所以邻接表的size设为最大值100就够了)
    list<int> *adj = new list<int>[100];

    //输入m行,初始化邻接表
    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;
            adj[parent].push_back(child);
        }
    }
    list<int> nodeList;
    nodeList.push_back(1);
    list<int> resultList;
    //逐层处理
    while(true){
        if (nodeList.empty()){
            //empty意味着到达了最深处,不需要继续了
            break;
        }
        int leafCount = 0;
        //下一层所有节点的集合
        list<int> nextLevelNodeList;
        //循环单层所有节点
        while(true) {
            if (nodeList.empty()){
                //这里的empty意味着这一层节点已经全部都判断完毕了
                break;
            }
            int node = nodeList.front();
            nodeList.pop_front();
            //判断是否有子节点,没有的话leafCount++,有的话,将child加到下一层的节点集合中,下一次遍历
            if(!adj[node].empty()){
                list<int>::iterator it;
                for(it= adj[node].begin(); it != adj[node].end(); it++){
                    nextLevelNodeList.push_back(*it);
                }
            } else {
                leafCount++;
            }

        }
        nodeList = nextLevelNodeList;
        resultList.push_back(leafCount);
    }

    if(!resultList.empty()){
        auto it = resultList.begin();
        cout << *it;
        it++;
        for(; it != resultList.end(); it++){
            cout << " " << *it;
        }
    }


}

posted on 2020-09-06 11:15  mindSucker  阅读(87)  评论(0编辑  收藏  举报