PAT甲级1004CountingLeaves

来源:互联网 发布:html登录注册界面源码 编辑:程序博客网 时间:2024/05/17 03:47

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

第4题Counting Leaaves,查找家族树中没有子节点的结点个数,理解题目要求,先输入总人数,再输入有后代的结点个数,本题要点在于存储家族树和统计不同层级的符合条件的结点个数,我选择使用邻接矩阵进行存储,然后用深度搜索统计所符合的结点。为了编写方便,我选用了vector来存储邻接矩阵,做了点空间优化。

#include <iostream>#include <vector>using std::cout;using std::cin;using std::endl;using std::vector;// 全局变量,记录家族树的深度int maxLevel = 0;// 输入树void Input(vector<vector<int>>& tree, const int N) {    int people = 0; // 本人编号    int childCount = 0; // 孩子个数    int children = 0; // 孩子编号    vector<int> _tree; // 本人的孩子数列    for (int i = 0; i < N; i++) {        _tree.clear(); // 对树清空        cin >> people >> childCount; // 输入编号和个数        for (int j = 0; j < childCount; j++) {            cin >> children;            _tree.push_back(children); // 加入,这样可以避免空间浪费        }        // 将对应编号的树设置为得到的vector        tree[people] = _tree;    }}// 进行查找,使用了递归来进行深度搜索// tree树,people要查找的编号,level他的深度,result用来存储结果的vectorvoid Count(const vector<vector<int>>& tree, const int people, const int level, vector<int>& result) {    bool hasChild = false; // 将有孩子置为假    maxLevel = maxLevel > level ? maxLevel : level; // 得到最大深度    // 对其数列开始遍历,没有孩子将会直接结束    for (auto i = 0; i < tree[people].size(); ++i) {            hasChild = true;            // 对孩子递归搜索,level加一            Count(tree, tree[people][i], level + 1, result);    }    // 如果没有孩子,满足条件,对应深度    if (!hasChild) {        ++result[level];    }}// 主函数int main(void) {    int people = 0;    int lines = 0; // 行数    cin >> people >> lines;    // 方便起见,本向量个数设置为people+1,而第二位置空是为了动态添加,不同于平时的邻接矩阵    vector<vector<int>> tree(people + 1, vector<int>());    Input(tree, lines);    // 没有开始搜索的时候是不知道深度的,直接设置为people+1    vector<int> result(people + 1, 0);    // 开始搜索    Count(tree, 1, 1, result);    // 输出结果    cout << result[1];    for (int i = 2; i <= maxLevel; i++) {        cout << " " << result[i];    }    cout << endl;    return 0;}

算法很重要

0 0
原创粉丝点击