题目描述

链接
给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径

分析

  • 静态数组建树
  • dfs遍历:注意什么地方要回溯状态
  • 二维数组排序,基本用vector实现,记住以后写begin和end了!如果用vector的话
bool cmp(vector<int> a, vector<int> b){int len = min(a.size(),b.size());for(int i=0;i<len;i++){if(a[i] == b[i]) continue}
}
sort(ans.begin(), ans.end(), cmp);
  • 当然最后这里还是用了一维数组排序,因为有个点过不了,很奇怪
#include<bits/stdc++.h>
using namespace std;const int maxn = 105;
struct node{int w;vector<int> child;
}nodes[maxn];bool cmp(int a, int b){return nodes[a].w > nodes[b].w;
}int n,m,s,k,id;int sum;
vector<vector<int> > ans;
vector<int> path;
void dfs(int i){path.push_back(nodes[i].w);sum += nodes[i].w;if(nodes[i].child.size()==0){if(sum == s){ans.push_back(path);}path.pop_back();sum -= nodes[i].w;return;}for(int j=0;j<nodes[i].child.size();j++){dfs(nodes[i].child[j]);}path.pop_back();sum -= nodes[i].w;
}int main(){cin>>n>>m>>s;for(int i=0;i<n;i++){cin>>nodes[i].w;}for(int i=0;i<m;i++){cin>>id>>k;for(int j=0;j<k;j++){int tmp;cin>>tmp;nodes[id].child.push_back(tmp);}sort(nodes[id].child.begin(), nodes[id].child.end(), cmp);}dfs(0);for(int i=0;i<ans.size();i++){for(int j=0;j<ans[i].size();j++){if(j==0) cout<<ans[i][j];else cout<<" "<<ans[i][j];}cout<<endl;}}

转载于:https://www.cnblogs.com/doragd/p/11269755.html

PAT A1053 Path of Equal Weight [树的DFS遍历]相关推荐

  1. [PAT] A1053 Path of Equal Weight

    (要熟练!)(树的遍历) 题目大意 (题目链接)https://pintia.cn/problem-sets/994805342720868352/problems/99480542415328051 ...

  2. 【PAT】A1053 Path of Equal Weight

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

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

    文章目录 题目分析 题目链接 题目分析 输入样例: 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 ...

  4. 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 ...

  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. 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 ...

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

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

  8. 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 ...

  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. P1803 凌乱的yyy / 线段覆盖(选择不相交区间,贪心)
  2. ARM通用NEON多媒体处理SIMD引擎
  3. Linux 下的动态链接库问题
  4. C语言如何产生随机数
  5. 下1000个用户的云计算革命
  6. 95-190-540-源码-window-清除器(Evictors)-简介
  7. oracle如何验证导入完整,关于Oracle 9i导入/导出效果的测试报告
  8. dev-cpp的安装教程
  9. 无线传感器网络中竞争性MAC协议
  10. Beer Barrels
  11. Express + JWT用户认证最轻实践
  12. 显卡内存和计算机内存,显存和内存的关系和区别
  13. java月份下拉菜单_实现一个日期下拉菜单
  14. 各位大神,有没有类似于百度云软件开始时的设置向导的例子呀
  15. C语言课题1:猜数字游戏【从基础开始手把手教你】
  16. 《梵高》-孤独的天才
  17. 汇编语言实现 大小写字母转换
  18. [转载] 苹果 AppStore 应用商店生存之道
  19. IDM一直弹出下载框怎么办?
  20. 每日任务:1276. 不浪费原料的汉堡制作方案

热门文章

  1. qt插件开发python_Qt5 Matplotlib设计器插件
  2. python简单的爬虫程序代码_简单的电子邮件爬虫Python代码
  3. mysql sum 对应_ASK MySQL查询SUM不同的表
  4. c++ list排序_STL之list
  5. 如何在Win7旗舰版中安装并设置IIS
  6. python 流写入文件_Python数据流写入文件
  7. 加州理工学院公开课:机器学习与数据挖掘_神经网络
  8. oracle vm 加大存储,Oracle VM VirtualBox下ubuntu虚拟机存储空间不够解决方案
  9. java访问win10共享盘失败_Win10不能访问共享磁盘的解决方法(亲测能用)
  10. 7z001怎么解压在安卓手机上面_安卓手机怎么设置网易企业邮箱