1004 Counting Leaves (30)(30 分)



1 题目


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

Input

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.

Output

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

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184

2 解题思路

  给你一个家谱,找到没有孩子的人。换言之,该题用来统计树结构中的叶子结点,可以采用深度优先遍历算法来进行统计。
  我们采用vector数组作为树的存储结构,std提供了较为强大的函数方便使用。将树结点逐一输入到vector 中,每一个元素存储了该结点的子孩子信息,自动向下探索子孩子,直到vector[i].size()==0表明该结点的子孩子为零,即叶子结点。
  可以采用循环结构来进行上述遍历,访问每一个结点,也可以使用递归结构。同时,每往下探测一层,需要一个变量来统计所在的层次数,以便统计每层的叶子结点个数。

3 AC代码

/*
** @Brief:No.1004 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-7-3 
*/

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> v[100];
int book[100];
int maxDepth=-1; 

void dfs(int index,int depth){
    // 此时第index结点已经没有子结点了,则表明是叶子结点 
    if(v[index].size()==0){
        // 统计该叶子结点 
        book[depth]++;
        // 更新最大层次 
        maxDepth = max(maxDepth,depth);
        return ;
    }
    // 递归调用 
    for(int i=0;i<v[index].size();i++){
        dfs(v[index].at(i),depth+1);
    }
} 
int main(){
    int N,M;
    int node,K;
    while(cin>>N>>M){
        for(int i=0;i<M;i++){
            cin>>node>>K;
            for(int j=0;j<K;j++){
                int temp;
                cin>>temp;
                v[node].push_back(temp);
            }
        }

        // 从第一个结点开始,第零层 
        dfs(1,0);

        cout<<book[0];
        for(int i=1;i<=maxDepth;i++){
            cout<<" "<<book[i];
        } 
    }
    return 0;
}

  还可以采用广度优先遍历的方法进行统计:
  借助一个Queue 将每个结点的子结点的ID按顺序入队,然后遍历每一个结点的所有子结点,对于vector.size()==0的情况,判定其为叶子结点,并做统计。递归执行上述过程,直到所有的结点都出队。
  注意,广度优先遍历和层次遍历具有相同的性质,和深度遍历不同,深度遍历向下遍历,便于统计层次,而广度优先难以计算层次结构,因此,需要一个额外的层次数组来计算每一个结点所在的层次。

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int level[100];// 保存当前结点的所在层数 
int book[100];// 记录每层 
int maxlevel = -1;
vector<int> v[100];
void bfs() {
    queue<int> q;
    q.push(1);
    level[1] = 0;
    while(!q.empty()) {
        // 获取队首元素 
        int index = q.front();
        q.pop();
        // 更新最大层数 
        maxlevel = max(level[index], maxlevel);
        if(v[index].size() == 0)
            book[level[index]]++;// 统计所在层数的叶子结点 
        // 对index结点的所有子结点进行遍历 
        for(int i = 0; i < v[index].size(); i++) {
            q.push(v[index][i]);// 子结点入队 
            level[v[index][i]] = level[index] + 1;// 同时记录结点的所在层次 
        }
    }
}
int main() {
    int n, m, k, node, c;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < m; i++) {
        scanf("%d %d",&node, &k);
        for(int j = 0; j < k; j++) {
            scanf("%d", &c);
            v[node].push_back(c);
        }
    }
    bfs();
    printf("%d", book[0]);
    for(int i = 1; i <= maxlevel; i++)
        printf(" %d", book[i]);
    return 0;
}

4 总结

  以前对《数据结构》的学习,仅仅停留在“自然语言”阶段,能够画图描述,能够做做题,但是不太会用代码描述,更没有在工程中实际应用过。懂,而不会做,无法在实际编程中产生战斗力,这是一件遗憾的事,希望以后能把之前缺的动手能力,补回来。

猜你喜欢

转载自blog.csdn.net/cv_jason/article/details/80901866