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(){
        //节点数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;
            }
        }
    
    
    }
    
  • 相关阅读:
    第一节:RestfulCRUD案例
    第三节:<mvc:annotationdriven />标签解析
    第二节:SpringMVC 的表单标签
    第一节:SpringMVC—数据绑定流程分析
    第四章:(2)Web开发 之 webjars&静态资源映射规则
    第三节:处理静态资源
    10.05
    四则运算二柱子1
    10.08
    10.02
  • 原文地址:https://www.cnblogs.com/IC1101/p/13621060.html
Copyright © 2011-2022 走看看