【PAT甲级题解记录】1004 Counting Leaves (30 分)
前言
Problem:1004 Counting Leaves (30 分)
Tags:树的DFS
Difficulty:剧情模式
想流点汗想流点血死而无憾Address:1004 Counting Leaves (30 分)
问题描述
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
有一棵树,节点编号 01~99 ,求从根节点所在层开始按顺序所有层的叶子节点数量。
解题思路
- 其实算是树的遍历,但不需要花心思写太多数据结构,只需要拿一个二维数组去存取每一个节点的子节点集合。
- 遍历方法可以采取递归dfs较为简单,搜索到叶子节点时对相应层的叶子数累加即可。
- 才知道 ‘01’ 这种作为整型输入时可以直接输入为1。
参考代码
/*
* @Author: Retr0.Wu
* @Date: 2022-02-13 00:23:45
* @Last Modified by: Retr0.Wu
* @Last Modified time: 2022-02-13 00:58:09
*/
#include <bits/stdc++.h>
using namespace std;
vector<int> cnt(100); // 存储每一层的叶子结点数量(最多100个点自然最多100层)
vector<int> son[100]; // 存储每一个节点的子结点集合
int maxdepth;
void dfs(int father,int depth)
{
if(son[father].empty()){ // 是一个叶子结点,该层cnt++
cnt[depth]++;
maxdepth = max(maxdepth,depth); // 只在叶子结点处需要判断,有子节点的肯定子节点深度更深所以不用max()
}
else{ // 否则往下遍历
for(int i=0;i<son[father].size();i++){
dfs(son[father][i],depth+1); // 千万不要depth++/++depth不然depth在这个循环里会一直变大
}
}
}
int main()
{
int N, M; // 100
cin>>N>>M;
for (int i = 0; i < M; i++)
{
int ID,K;
cin>>ID>>K; // 01 这种形式也可以直接当整数输入变成 1
for(int j=0;j<K;j++){
int idi = 0;
cin>>idi;
son[ID].push_back(idi);
}
}
dfs(1,0);
for(int i=0;i<=maxdepth;i++){
if(i>0) cout<<" ";
cout<<cnt[i];
}
return 0;
}
如果想将id作为字符串处理,可以这么写:
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int N; // number of nodes (100)
int M; // the number of non-leaf nodes
map<string, vector<string>> tree;
vector<int> leaf_num(100, 0);
int MAX_level;
void init() {
cin >> N >> M;
for (int i = 0; i < M; ++i) {
string id;
int k;
cin >> id >> k;
vector<string> children(k);
for (int j = 0; j < k; ++j) {
cin >> children[j];
}
tree[id] = children;
}
}
void dfs(string root, int depth) {
if (tree.count(root)) {
vector<string> children = tree[root];
for (int i = 0; i < children.size(); i++) {
dfs(children[i], depth + 1);
}
} else {
leaf_num[depth]++;
MAX_level = max(MAX_level, depth);
}
}
void solution_1004() {
init();
dfs("01", 0);
for (int i = 0; i <= MAX_level; i++) {
if (i > 0)cout << " ";
cout << leaf_num[i];
}
cout << endl;
}
int main() {
solution_1004();
return 0;
}
总结
简单的树的深搜。