文章目录

  • 题目分析
  • 题目链接

题目分析



输入样例:

20 9 24
10 2 4 3 5 10 2 18 9 7 2 2 1 3 12 1 8 6 2 2
00 4 01 02 03 04
02 1 05
04 2 06 07
03 3 11 12 13
06 1 09
07 2 08 10
16 1 15
13 3 14 16 17
17 2 18 19

输出样例:

10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2

题意重述:给定一颗二叉树和一个路径长度s,让遍历二叉树,找出所有长度等于s的路径,并且输出。
分析:使用邻接表来存树。 然后就是dfs来遍历。

当遇到叶子结点h[u] == -1的时候,如果长度等于s则保存一条路径,并且dfs递归返回。

另外就是深度优先遍历邻接表。其中e[i]存的是结点,不是i。

 //暴搜u的所有儿子for(int i=h[u]; i!= -1 ;i=ne[i]){path.push_back(w[e[i]]); //更新路径dfs(e[i],s+w[e[i]],path);    //暴搜下一个点path.pop_back(); //恢复现场}

ac代码

#include<bits/stdc++.h>
using namespace std;
const int N = 110;int n , m,S;
//邻接表
int h[N],e[N],ne[N],idx;int w[N]; //权重vector<vector<int>> ans; //记录路径// e[i]存的是当前结点
//ne[i] 存的是单链表中下一个结点“序号”
//idx 就是单链表中的“序号”
void add(int a, int b){e[idx] =b, ne[idx]= h[a],h[a] =idx ++;
}/*暴搜所有等于S的路径u:当前结点s:权值path:路径
*/
void dfs(int u,int s , vector<int>& path){//递归结束条件if(h[u] == -1){ // 是叶子结点if(s == S) {ans.push_back(path);return;}}//暴搜u的所有儿子for(int i=h[u]; i!= -1 ;i=ne[i]){path.push_back(w[e[i]]); //更新路径dfs(e[i],s+w[e[i]],path);    //暴搜下一个点path.pop_back(); //恢复现场}}int main(){memset(h, -1, sizeof h);cin>> n  >> m >> S;for(int i = 0; i < n; i++) cin>>w[i];//i号结点的权重是w[i]while(m--){int id , n1;cin>> id >> n1;while(n1--){ //id结点的儿子们int son;cin >> son;add(id ,son);}}//根结点int root = 0;//初始化路径为根结点vector<int> path({w[0]});//暴搜dfs(root,w[0],path);//排序sort(ans.begin(),ans.end(),greater<vector<int>>());//输出for(auto p :ans){cout<<p[0];for(int i=1;i<p.size();i++) cout<<" "<<p[i];cout<<endl;}}

题目链接

acwing1539. 等重路径
PAT甲级1053 Path of Equal Weight (30分)

PAT甲级1053 Path of Equal Weight (30分) :[C++题解]dfs求树的路径长度、邻接表相关推荐

  1. PAT甲级 -- 1053 Path of Equal Weight (30 分)

    Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. The weig ...

  2. PAT (Advanced Level) Practice 1053 Path of Equal Weight (30 分)

    1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigned to ...

  3. 1053 Path of Equal Weight (30分)

    1053 Path of Equal Weight (30分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...

  4. PAT:1053. Path of Equal Weight (30) AC

    #include<stdio.h> #include<vector> #include<queue> #include<algorithm> using ...

  5. 1053 Path of Equal Weight

    1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned ...

  6. PAT甲级1127 ZigZagging on a Tree (30分):[C++题解]之字形层次遍历树bfs实现一层一层读入

    文章目录 题目分析 题目链接 题目分析 分析 给定中序遍历和后序遍历,先建树: 后面就是写一个bfs,层序遍历稍微改改即可. bfs要做的修改是如何一层一层的读入.令根结点是第一层,本题的之字形遍历就 ...

  7. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

  8. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

  9. 1053 Path of Equal Weight (30 分)

    题目 Given a non-empty tree with root R, and with weight WiW_iWi​ assigned to each tree node TiT_iTi​. ...

最新文章

  1. 2022-2028年中国摩托车行业投资分析及前景预测报告(全卷)
  2. LINUX环境下资源下载中文目录及中文文件名称问题
  3. 占据栅格地图(Occupancy Grid Map)
  4. 生产环境LNMP (果图片)
  5. 什么是main方法?
  6. linux系统如何进行录屏
  7. 腾讯2019暑期实习生提前批CV岗笔试题
  8. 【解题报告】Leecode 700. 二叉搜索树中的搜索——Leecode每日一题
  9. servlet中弹出网页对话框
  10. 领酌酒业:一文阅尽酱香酒
  11. 今日讨论:你们测试组有公共用例库吗?
  12. 负离子程序员的一组未来手绘,酷毙了
  13. 工程数学线性代数第六版答案与解析,《线性代数附册学习辅导与习题全解》
  14. 计算机网络基础肖盛文电子书,网络实用教程
  15. 折腾:如何让你的老电脑快起来
  16. 如何将扫描PDF文件转换成word,两个超简单的方法一看就会
  17. 解决打包过程中出现 ModuleNotFoundError: No module named ‘pydicom.encoders.gdcm‘ 报错的方法
  18. 服务拆分的设计和思考(B2B 技术共享第九篇)
  19. 系统开发系列 之MyEclipse创建WebService详细教程和调用教程(spring框架+maven+CXF框架)
  20. python+django加载静态网页模板

热门文章

  1. 12、Struts2表单重复提交
  2. Flash学习笔记(01)
  3. 深入super,看Python如何解决钻石继承难题
  4. 通向高可扩展性之路(谷歌篇)
  5. 使用img.src跨域请求
  6. 初中数学分几个模块_【初中数学】8大模块61个必考易错知识点!
  7. php图片传入及改名代码,WordPress添加媒体中文名图片上传改名(优化版)
  8. 【数理知识】标量函数、二次型函数、矩阵、正定负定半正定半负定
  9. 2.8 Adam 优化算法-深度学习第二课《改善深层神经网络》-Stanford吴恩达教授
  10. 4.8 这和大脑有什么关系-深度学习第一课《神经网络与深度学习》-Stanford吴恩达教授