徐子烊

  • 主页
  • 语雀
所有文章 关于我

徐子烊

  • 主页
  • 语雀

1004 Counting Leaves (30 分)

2019-03-05

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.

Input Specification:

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:

1
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 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:

1
2
2 1
01 1 02

Sample Output:

1
0 1

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

题目大意

给出n,m代表树的节点数以及非叶结点树,接下来输入m行,每行格式id k id[1] id[2]..代表id结点的k个儿子。要求层序输出各层无儿子结点的数量。

分析

利用vector<int>数组存储各结点的儿子号,利用lay数组记录结点所在的层数,利用bfs输出结果

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>
#include <cstring>
using namespace std;

int main() {
int n,m,cur,k,t,maxlay=-1;
int lay[110]={},res[110]={};
cin>>n>>m;
if(n==0)
return 0;
vector<int> vec[110];
for(int i=0;i<m;i++){
cin>>cur>>k;
for(int j=0;j<k;j++){
cin>>t;
vec[cur].push_back(t);
}
}
lay[1]=0;
deque<int> que;
que.push_back(1);
while(!que.empty()){
cur=que.front();
que.pop_front();
//记录树的深度
maxlay=max(maxlay,lay[cur]);
if(vec[cur].empty()){//该结点无儿子
res[lay[cur]]++;
}
else
for(int a:vec[cur]){//将该结点的儿子入队,儿子的层数=父亲层数+1
que.push_back(a);
lay[a]=lay[cur]+1;
}
}
cout<<res[0];
for(int i=1;i<=maxlay;i++)
cout<<" "<<res[i];
return 0;
}
  • PAT
  • 树
  • PAT题解

扫一扫,分享到微信

微信分享二维码
1007 Maximum Subsequence Sum (25 分)
1003 Emergency (25 分)
  1. 1. 1004 Counting Leaves (30 分)
    1. 1.1. Input Specification:
    2. 1.2. Output Specification:
    3. 1.3. Sample Input:
    4. 1.4. Sample Output:
  2. 2. 题目大意
  3. 3. 分析
  4. 4. 代码
© 2020 徐子烊
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • PAT
  • 多项式
  • dijkstra
  • 图
  • 树
  • 最大子序列
  • 进制转换
  • 二分
  • dfs
  • 连通分量
  • 模拟
  • STL
  • 大整数
  • 需复习
  • 链表
  • 贪心
  • 动态规划
  • 子序列
  • 二叉树
  • LCS
  • 栈
  • 排序
  • 质因数
  • 树状数组
  • 完全二叉树
  • 整数溢出
  • AVL树
  • 深搜
  • 哈希
  • bfs
  • 并查集
  • 红黑树
  • LCA
  • 拓扑排序
  • lca
  • 实习
  • bat
  • Leetcode
  • Java并发
  • 线程池
  • 6.824
  • 分布式
  • mapreduce
  • caffe
  • epoll
  • Raft
  • schnorr
  • 密码学
  • 设计模式
  • 单例模式
  • 原型模式
  • 工厂模式
  • 建造者模式
  • 抽象工厂
  • 桥接模式
  • 模版模式
  • 组合模式
  • 策略模式
  • 装饰器模式
  • 迭代器模式
  • 适配器模式

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

很惭愧

只做了一点微小的工作