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

PAT树的深度优先搜索(dfs)和广度优先搜索(bfs)

dfs和bfs都是常用的搜索算法,结合PAT1094,简单动手分别以dfs和bfs实现树的遍历吧。

1094 The Largest Generation (25 分)

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (<100) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 to N), and M (<N) which is the number of family members who have children. Then M lines follow, each contains the information of a family member in the following format:

ID K ID[1] ID[2] ... ID[K]

whereIDis a two-digit number representing a family member,K(>0) is the number of his/her children, followed by a sequence of two-digitID's of his/her children. For the sake of simplicity, let us fix the rootIDto be01. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the largest population number and the level of the corresponding generation. It is assumed that such a generation is unique, and the root level is defined to be 1.

Sample Input:

23 13 21 1 23 01 4 03 02 04 05 03 3 06 07 08 06 2 12 13 13 1 21 08 2 15 16 02 2 09 10 11 2 19 20 17 1 22 05 1 11 07 1 14 09 1 17 10 1 18

Sample Output:

9 4

题目大意:给你一棵树,让你找出哪一层的节点个数最多,输出节点个数和对应的层数。

dfs:

#include<cstdio> #include<iostream> #include<vector> const int maxn=105; vector<int> tree[maxn]; int sum[maxn];//记录每一层的节点个数 int n,m; void dfs(int level,int node){ sum[level]++; for(int i=0;i<tree[node].size();i++){ dfs(level+1,tree[node][i]); } } int main() { cin>>n>>m; for(int i=0;i<m;i++){ int id,num; cin>>id>>num; for(int j=0;j<num;j++){ int child; cin>>child; tree[id].push_back(child); } } dfs(1,1); int maxnum=-1,maxlevel=-1; for(int i=0;i<maxn;i++){ if(sum[i]>maxnum){ maxnum=sum[i]; maxlevel=i; } } cout<<maxnum<<" "<<maxlevel<<endl; return 0; }

bfs

#include<cstdio> #include<iostream> #include<vector> #include<queue> using namespace std; const int maxn=105; vector<int> tree[maxn]; int sum[maxn]; int level[maxn]; //记录节点对应的层数 int n,m; void bfs(){ queue<int> q; q.push(1); level[1]=1; while(!q.empty()){ int node=q.front(); q.pop(); sum[level[node]]++; for(int i=0;i<tree[node].size();i++){ level[tree[node][i]]=level[node]+1; q.push(tree[node][i]); } } } int main() { cin>>n>>m; for(int i=0;i<m;i++){ int id,num; cin>>id>>num; for(int j=0;j<num;j++){ int child; cin>>child; tree[id].push_back(child); } } bfs(); int maxnum=-1,maxlevel=-1; for(int i=0;i<maxn;i++){ if(sum[i]>maxnum){ maxnum=sum[i]; maxlevel=i; } } cout<<maxnum<<" "<<maxlevel<<endl; return 0; }
http://www.jsqmd.com/news/1281684/

相关文章:

  • Karpathy 65行提示词:重构AI编程协作流程,提升代码质量与效率
  • 大模型应用开发的工程化路线:从实验到生产的七个里程碑
  • Google发布网络安全报告 工具堆够用了吗
  • 杭州西湖区漏水检测维修一站式服务 - 本地正规防水补漏公司精选推荐(2026 最新)全域上门:卫生间 / 厨房 / 阳台 / 屋顶渗漏水免砸砖检测维修补漏全攻略 - 吉林同城获客
  • 卡文断更怎么办?10款高效写小说软件横评(含防踩雷指南)
  • 发票、凭证和对账单怎么整理成 Excel?先定义字段,再逐项复核
  • 云南24小时道路救援公司推荐,汽车搭电救援公司哪家好?2026年7月富宁美星服务网点信息核对指南 - geo88
  • 如何高效应用Poppins字体:面向初学者的完整实践指南
  • 通用AI与垂直领域融合的技术实践与优化
  • 张家港上门回收废品公司推荐,汽车电池回收公司哪家好避坑指南:4个常见坑+5条硬标准,2026靠谱公司推荐 - geo88
  • 微前端框架 2026 选型对比:qiankun 的沙箱困境、Module Federation 的共享模型与 wujie 的降级智慧
  • 长鑫上市阿里浮盈超 1600 亿!投资风格转变,从控制转向“在场”
  • 2026年|外贸人必看!热门外贸独立站谷歌SEO服务商深度测评
  • 终极指南:三步掌握国家中小学智慧教育平台电子课本PDF下载技巧
  • TV Bro电视浏览器:10个实用技巧让智能电视变身全能上网设备
  • Visual C++运行库终极修复指南:如何5分钟解决所有Windows软件兼容性问题
  • mybatis 分批次手动提交事务
  • Python核心知识体系思维导图
  • 26 款别克至境 E7 车灯升级案例|米格国际 4 矩阵模组无损改装,夜间行车照明拉满 - 英特菲斯
  • 实验报告撰写规范与核心内容梳理指南
  • MP2672A与MKV42F256VLH16在锂离子电池均衡系统中的应用
  • 大模型与算法面试宝典:Transformer与RAG核心考点解析
  • Next.js性能调优与压力测试实战指南
  • Linux环境编译Hadoop源码包
  • 2026年7月新加坡中介机构选哪家丨多家热门机构信息梳理 - 资讯纵览
  • Avidemux2开源视频编辑器深度解析:跨平台架构与核心技术实现
  • Go入门:main包与main函数的特殊地位
  • 领优惠券APP数据中台建设:GMV、佣金与用户留存率的实时数据监控体系
  • 科研文献高效检索技巧与资源平台解析
  • 盘点0Ω电阻容易被忽略的四类工业级实用设计