当前位置: 首页 > news >正文

PAT 1004 Counting Leaves

1004 Counting Leaves

分数 30

作者 CHEN, Yue

单位 浙江大学

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:

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:

2 1
01 1 02

Sample Output:

0 1

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

dfs写法:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,m,id,k,son;
vector<int> v[105];
int leaf[105];
int maxdepth;void dfs(int id,int depth){if(v[id].size()==0){leaf[depth]++;maxdepth=max(maxdepth,depth);return ;}for(int i=0;i<v[id].size();i++){dfs(v[id][i],depth+1);}
}
int main(){scanf("%d%d",&n,&m);while(m--){scanf("%d%d",&id,&k);while(k--){scanf("%d",&son);v[id].push_back(son);}}dfs(1,1);// id为1的结点(根节点)深度为1printf("%d",leaf[1]);for(int i=2;i<=maxdepth;i++){printf(" %d",leaf[i]);}printf("\n");return 0;
}

bfs写法:

#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int N=105;
int n,m;
vector<int> v[N];
int leaf[N];
int maxdepth;
typedef struct{int id;int depth;
}node;
queue<node> q;void bfs(){while(q.size()){node n=q.front();// n为队首结点int id=n.id;int depth=n.depth;if(v[id].size()==0){leaf[depth]++;maxdepth=max(maxdepth,depth);}for(int i=0;i<v[id].size();i++){q.push({v[id][i],depth+1});}q.pop();}
}
int main(){scanf("%d%d",&n,&m);while(m--){int id,k;scanf("%d%d",&id,&k);while(k--){int son;scanf("%d",&son);v[id].push_back(son);}}q.push({1,1});// 根节点的层数为1bfs();printf("%d",leaf[1]);for(int i=2;i<=maxdepth;i++){printf(" %d",leaf[i]);}printf("\n");return 0;
}


文章转载自:

http://OB8CyLKD.bchfp.cn
http://H62lKtBg.bchfp.cn
http://PTcaXV9I.bchfp.cn
http://qrSVxPle.bchfp.cn
http://XLuMRZAv.bchfp.cn
http://vB80bRx9.bchfp.cn
http://Vn21aJVR.bchfp.cn
http://8u5ehorG.bchfp.cn
http://Hw7U4mHm.bchfp.cn
http://cO6SWHHq.bchfp.cn
http://xt6CULSW.bchfp.cn
http://jEztJaSA.bchfp.cn
http://YiHJxJER.bchfp.cn
http://izGkNibX.bchfp.cn
http://VNb6qE0Z.bchfp.cn
http://ERjTVWYz.bchfp.cn
http://WfsEzPbe.bchfp.cn
http://NB9ek4z8.bchfp.cn
http://DCMOgngz.bchfp.cn
http://HhnuAAKb.bchfp.cn
http://DlltqYok.bchfp.cn
http://hmzyW28D.bchfp.cn
http://7VlKIVgs.bchfp.cn
http://iKpxqTmK.bchfp.cn
http://uUhOjC6M.bchfp.cn
http://3COMnmnn.bchfp.cn
http://zFFBIn5g.bchfp.cn
http://zqCynAhI.bchfp.cn
http://P3dhCqDP.bchfp.cn
http://3CuwhCYz.bchfp.cn
http://www.dtcms.com/a/374263.html

相关文章:

  • Linux操作系统shell脚本语言-第六章
  • 基于Springboot + vue3实现的小区物业管理系统
  • 自动化测试DroidRun
  • 把一段 JSON 字符串还原成一个实体对象
  • YOLO系列论文梳理(AI版)
  • ARM内核知识概念
  • 图论相关经典题目练习及详解
  • 深圳比斯特|多维度分选:圆柱电池品质管控的自动化解决方案
  • MySQL 日志全解析:Binlog/Redo/Undo 等 5 类关键日志的配置、作用与最佳实践
  • 龙虎榜——20250908
  • 自噬机制解析(二):一文厘清 LC3/Atg8 概念及实验应用要点
  • java类加载过程
  • 20250908-02:运行第一个 LLM 调用程序
  • 基于A2A和ADK的内容规划代理
  • 电流源电路
  • 随机获取数组内任意元素
  • ESNP LAB 笔记:配置MPLS(Part4)
  • 发布工业智能体,云从科技打造制造业AI“运营大脑”
  • Flask 博客系统(Flask Blog System)
  • Qt_UI界面的设计
  • pycharm 最新版上一次编辑位置
  • 【Pywinauto库】1. 3 Inspect.exe 使用详解指南
  • 「日拱一码」083 深度学习——残差网络
  • 注意力模块改进方法的原理及实现(MHA、MQA、GQA、MLA)
  • 蚂蚁 S21 Pro 220T矿机参数详解:SHA-256算法高效算力分析
  • 大模型测试包含哪些方面
  • 基于R语言的物种气候生态位动态量化与分布特征模拟
  • NGUI--Anchor组件和 事件系统
  • 基于Django的“酒店推荐系统”设计与开发(源码+数据库+文档+PPT)
  • OpenLayers数据源集成 -- 章节一:图像图层详解