zoukankan      html  css  js  c++  java
  • PAT甲级——A1004 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, the number of nodes in a tree, and M (<), 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 01level, 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

    即遍历整颗数,使用DFS或者DFS
    用数组记录每个节点的子节点是谁
     1 #include <iostream>
     2 #include <vector>
     3 #include <queue>
     4 
     5 using namespace std;
     6 
     7 //给出一棵树,问每一层的叶子结点数量
     8 //使用BFS或者DFS
     9 
    10 vector<vector<int>>nodes(1001);
    11 vector<int>depth(1001);
    12 int maxDepth = -1;
    13 
    14 void DFS(int index, int h)
    15 {
    16     maxDepth = maxDepth > h ? maxDepth : h;
    17     if (nodes[index].size() == 0)//data[index].size() == 0)//即为叶子结点
    18         depth[h]++;//层数
    19 
    20     for (int i = 0; i < nodes[index].size(); ++i)
    21         DFS(nodes[index][i], h + 1);
    22 }
    23 
    24 void BFS( )
    25 {
    26     queue<int>q;
    27     q.push(1);
    28     vector<int>level(1001, 0);//记录节点层数
    29     while (!q.empty())
    30     {
    31         int index = q.front();
    32         q.pop();
    33         maxDepth = maxDepth > level[index] ? maxDepth : level[index];//存储最大的层数
    34         if (nodes[index].size() == 0)//此节点为叶子节点
    35             depth[level[index]]++;//之所以要记录每个节点的层数,是因为,同一层有多个节点
    36         for (int i = 0; i < nodes[index].size(); ++i)
    37         {
    38             level[nodes[index][i]] = level[index] + 1;//孩子结点层数比父节点多一层
    39             q.push(nodes[index][i]);//将其孩子全部存入
    40         }
    41     }
    42 }
    43 
    44 
    45 
    46 int main()
    47 {
    48     int N, M;//N为节点数目
    49     cin >> N >> M;        
    50     for (int i = 0; i < M; ++i)
    51     {
    52         int ID, k, a;
    53         cin >> ID >> k;
    54         for (int j = 0; j < k; ++j)
    55         {
    56             cin >> a;
    57             nodes[ID].push_back(a);//即为一个节点底下所挂的子节点
    58         }
    59     }
    60 
    61     //DFS(1,0);
    62     BFS( );
    63     cout << depth[0];
    64     for (int i = 1; i <= maxDepth; ++i)
    65         cout << " " << depth[i];
    66     cout << endl;
    67 
    68     return 0;
    69 
    70 }
  • 相关阅读:
    实例属性 类属性 实例域 类域
    研究数据集
    static 静态域 类域 静态方法 工厂方法 he use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class 非访问修饰符
    accessor mothod mutator mothod 更改器方法 访问器方法 类的方法可以访问类的任何一个对象的私有域!
    上钻 下钻 切片 转轴 降采样
    识别会话
    Performance Tuning Using Linux Process Management Commands
    Secure Hash Algorithm 3
    grouped differently across partitions
    spark 划分stage Wide vs Narrow Dependencies 窄依赖 宽依赖 解析 作业 job stage 阶段 RDD有向无环图拆分 任务 Task 网络传输和计算开销 任务集 taskset
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11169839.html
Copyright © 2011-2022 走看看