目录

题目解释:

解题思路:

ac代码:

多看多做,才能更加熟悉树方面的知识!Very important!!


1053 Path of Equal Weight (30 分)

题目地址:https://pintia.cn/problem-sets/994805342720868352/problems/994805424153280512

题目解释:


对于每个结点,上面是结点的编号,用二位数字表示(two-digit),下面是该结点的权值,先寻找所有的路径,使路径上所有结点的权值之和是给定的s,并按非递减顺序输出这些路径上经过的权值。

如果对于两条路径,A1=B1,.....Ai-1=B i-1,Ai>Bi那么A路径比B路径大.

解题思路:


1)用path[i]存路径上第i个结点的编号,最后在一次输出编号对应的weight,即node[path[i]].weight

2)对于某个结点的子孩子,要先对这些子孩子的weight排序,即现遍历weight大的子孩子,最后也先输出weight大的子孩子,即实现了非递减得输出路径

3)dfs遍历树

ac代码:


#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define maxn 110
using namespace std;
struct NODE{int weight;vector<int> child;
}node[maxn];//结点数组
bool cmp(int a,int b)//weight从大到小输出,先排序
{return node[a].weight>node[b].weight;
}
int n,m,s;
int path[maxn];//path[i]表示路径上第i个结点的编号
void dfs(int index,int numnode,int sum)//访问结点为index,当前结点个数numnode,和sum
{if(sum>s) return ;if(sum==s){if(node[index].child.size()!=0)//非叶子结点return ;for(int i=0;i<numnode;i++){printf("%d",node[path[i]].weight);if(i<numnode-1) printf(" ");else printf("\n");}return ;}for(int i=0;i<node[index].child.size();i++){int child=node[index].child[i];path[numnode]=child;dfs(child,numnode+1,sum+node[child].weight);}
}
int main()
{scanf("%d%d%d",&n,&m,&s);for(int i=0;i<n;i++)scanf("%d",&node[i].weight);int id,k,child;for(int i=0;i<m;i++){scanf("%d%d",&id,&k);for(int j=0;j<k;j++){scanf("%d",&child);node[id].child.push_back(child);//向vector中存入孩子}sort(node[id].child.begin(),node[id].child.end(),cmp);//对每一个结点的子孩子们的weight排序}path[0]=0;dfs(0,1,node[0].weight);return 0;
}

多看多做,才能更加熟悉树方面的知识!Very important!!

PAT-A1053:Path of Equal Weight(普通树的遍历和非递减路径的输出)相关推荐

  1. [PAT] A1053 Path of Equal Weight

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

  2. PAT A1053 Path of Equal Weight [树的DFS遍历]

    题目描述 链接 给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径 分析 静态数组建树 dfs遍历:注意什么地方要回溯状态 二维数组排序,基本用v ...

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

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

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

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

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

  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. PAT:1053. Path of Equal Weight (30) AC

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

  10. 1079. Total Sales of Supply Chain (25)-PAT甲级真题(dfs,bfs,树的遍历)

    A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone invo ...

最新文章

  1. 【Qt】Qt再学习(二):Bars Example(Q3DBars)
  2. android阿里聚安全黑盒,阿里安全开放平台-阿里聚安全
  3. 数据结构——字符串(未完)
  4. 干货:如何在前端统计用户访问来源?
  5. Java程序设计基础--流程控制
  6. 爱因斯坦鲜为人知的另一面
  7. linux新手常用命令
  8. 矿井水质在线监测系统
  9. iOS开发多线程篇---线程间的通信
  10. 一个简易的C++性能分析工具
  11. 支付宝转账到银行卡/隐藏部分卡号
  12. 数据库_之常用API的使用
  13. Jzoj5605 Zkb
  14. html背景图片定位方法
  15. 高性能分布式缓存redis(持久化原理 安全策略 过期删除内存淘汰策略 性能压测 高可用 Redis Cluster)
  16. 曾经,被嫌弃的腾讯股权——读《腾讯传》
  17. win10提示无法通过电话激活怎么办?
  18. PCB如何添加矢量图形logo
  19. SEO系列三:周珍谈新手如何快速入门
  20. 计算机专业硕士毕业论文,计算机技术专业硕士毕业论文(9)(喜欢就下吧)...

热门文章

  1. 谈一下对VUE生命周期的理解
  2. layui框架简单解读,为什么要使用layui框架,layui框架的好处,后端程序员快速学习掌握layui框架技术型文档博文,layui学习笔记,快速掌握前端技术框架
  3. apollo @value没生效_有Apollo,还用Archaius吗?
  4. 转自weiphone]在美做开发多年,写给国内apple开发新手的一些心得,无教程
  5. 使用WebDeployment Project改善VS2005发布网站
  6. Cocos2d-x开发---改变父节点颜色、透明度影响子节点
  7. flask使用pymysql连接MySQL,生成xls文件并下载到本地
  8. StylesheetLanguage--如何使用Less--前端样式语言
  9. [Scikit-learn教程] 03.02 文本处理:分类与优化
  10. enlightenment提权的工具