PAT A1004 Counting Leaves(30)
来源:互联网 发布:福禄克网络测试 编辑:程序博客网 时间:2024/05/19 13:18
题意
- 给定家族树(结点总数、非叶节点总数和上下层对应关系),求每层的叶节点数。
注意
- 输入数据后从根节点递归得到每个节点的层次。(因为某些测试用例不是严格按照树层次输入的,不这样搞会挂掉一些测试点)
单词
- pedigree 血统,家谱
- hierarchy 层次
- fix 固定
代码
#include <iostream>using namespace std;const int MAX = 100;int n, m;struct P{ int ID; //1~99 int K; //孩子数目 int child[MAX]; int H; //层次(连续) 0~};P p[MAX];void count(){ int a[MAX] = { 0 }; int max_H = -1; for (int i = 1; i <= n; i++) { if (p[i].K == 0) a[p[i].H]++; //17.2.10 if (p[i].H > max_H) max_H = p[i].H; } for (int i = 0; i <= max_H; i++) { if (i == max_H) printf("%d", a[i]); else printf("%d ", a[i]); }}void r(int root){ if (p[root].K == 0) return; for (int i = 0; i < p[root].K; i++) { p[p[root].child[i]].H = p[root].H + 1; r(p[root].child[i]); }}int main(){ scanf_s("%d%d", &n, &m); for (int i = 1; i <= n; i++) p[i].K = 0; int pp; for (int i = 0; i < m; i++) { scanf_s("%d", &pp); scanf_s("%d", &p[pp].K); for (int j = 0; j < p[pp].K; j++) scanf_s("%d", &p[pp].child[j]); } p[1].H = 0; // r(1); count(); return 0;}
1 0
- PAT A1004 Counting Leaves(30)
- PAT A1004. Counting Leaves (30)
- a1004. Counting Leaves (30)
- A1004. Counting Leaves (30)
- PAT甲级练习题A1004. Counting Leaves
- 1004. Counting Leaves (30)-PAT
- pat 1004. Counting Leaves (30)
- 【PAT】1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- pat 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT:1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- [PAT]1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- PAT 1004. Counting Leaves (30)
- 一次客户端卡崩的沉思录
- Mevan之Jsoup解析器
- Android实战——LeakCanary检测内存泄漏
- 进程 线性地址到物理地址的映射
- 每天一个linux命令:find 命令概览
- PAT A1004 Counting Leaves(30)
- 使用SIMD技术提高C++程序性能
- 学习TDD(3)--实例2:基于ZooKeeper的服务器注册和探测类[分析篇]
- 范围查找
- 四种第三方登录
- 每天一个linux命令:find命令之exec
- 1st group anagram 49
- PART VIII 总结 Chapter 17 计算的限制
- 动规-区间DP模板