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 weight of a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to any leaf node L.

Now given any weighted tree, you are supposed to find all the paths with their weights equal to a given number. For example, let's consider the tree showed in the following figure: for each node, the upper number is the node ID which is a two-digit number, and the lower number is the weight of that node. Suppose that the given number is 24, then there exists 4 different paths which have the same given weight: {10 5 2 7}, {10 4 10}, {10 3 3 6 2} and {10 3 3 6 2}, which correspond to the red edges in the figure.

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, M (<N), the number of non-leaf nodes, and 0<S<2​30​​, the given weight number. The next line contains N positive numbers where W​i​​ (<1000) corresponds to the tree node T​i​​. 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 00.

Output Specification:

For each test case, print all the paths with weight S in non-increasing order. Each path occupies a line with printed weights from the root to the leaf in order. All the numbers must be separated by a space with no extra space at the end of the line.

Note: sequence {A​1​​,A​2​​,⋯,A​n​​} is said to be greater than sequence {B​1​​,B​2​​,⋯,B​m​​} if there exists 1≤k<min{n,m} such that A​i​​=B​i​​for i=1,⋯,k, and A​k+1​​>B​k+1​​.

Sample Input:

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

Sample Output:

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

Special thanks to Zhang Yuan and Yang Han for their contribution to the judge's data.

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 110;
struct node {int weight;vector<int> child;
}Node[maxn];bool cmp(int a, int b) {return Node[a].weight > Node[b].weight;
}//对每个结点的数据域进行从大到小的排序,注意()中是int不是node,return的是数组Node[].weight>Node[b].weightint n, m, S;//总结点数,非叶子结点数,给定的和
int path[maxn];//index为当前访问结点,numNode为当前路径上的结点个数
//sum为当前结点的权和void DFS(int index, int numNode, int sum) {if (sum > S)return;if (sum == S) {if (Node[index].child.size() != 0)return;//没到叶子结点//到达叶子结点,当前path[]中存放了一条完整的路径,输出他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);}sort(Node[id].child.begin(), Node[id].child.end(), cmp);}path[0] = 0;DFS(0, 1, Node[0].weight);return 0;
}

1053 Path of Equal Weight (30 分)一般树的遍历 DFS+vector容器+sort排序相关推荐

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

  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 分)

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

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

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

  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 分)

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

  8. 1053 Path of Equal Weight(超级无敌详细注释+45行代码)

    分数 30 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 Given a non-empty tree with root R, and with weight Wi​ assig ...

  9. 1053 Path of Equal Weight

    1. 以下两组关系很大的概念 树的深度优先搜索 - 先根遍历 - 递归 树的广度优先搜索 - 层序遍历 - 非递归 本题考察的是前者,我设置了这样一个结构体 struct Prestruct{int ...

  10. 1053 Path of Equal Weigh(甲级)

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

最新文章

  1. [CTO札记]MM晕倒地铁中--最适合的活动未必是阅读
  2. 独家 | 数据科学家对可复用Python代码的实用管理方法(附链接)
  3. mysql的1067启动错误的解决
  4. 一生只有43年,喜欢泡妹打架,却凭借一篇文章震惊世界,跻身一流数学家
  5. oracle 下和 db2的syscat 对应的,oracle db2命令对比(整理中)
  6. dalvik虚拟内存管理之二——垃圾收集
  7. sqli-labs(19)
  8. 比Spark更适合工业互联网的数据库——热门时序数据库介绍与核心文档汇总【施工中,欢迎留言加入】
  9. 公里导线时有没有计算机类计算方法,基于Matlab的导线网坐标计算. (1).doc
  10. 如何利用魔棒工具抠图_10秒教你如何用PS魔棒工具抠图
  11. 计算机网页的设计与应用的前言,网页设计前言.ppt
  12. Matlab科研绘图颜色补充(特别篇)—51种中国传统颜色
  13. ViewGroup详解
  14. php5.4.45连接mssql2000,用php在linux下连接mssql2000(转)
  15. 【2019暑假】市中小学生游泳比赛-酱油记By Chavapa
  16. JS中各种width和height的区别
  17. python远程监控服务器多个日志_flume远程监控一个文件
  18. 【Excel】给Excel生成工作表目录
  19. iOS开发中动画之点赞图标放大效果
  20. 龙芯电脑平台kubernetes集群编译及部署方案

热门文章

  1. Java集合类和HashMap遍历
  2. LeetCode_14_python_最长公共前缀
  3. pg库使用dblink连接mysql_PG-跨库操作-dblink
  4. Java基础,不需要复杂语句,使用for循环实现求出1~100之间的奇数和以及偶数和,超级简单
  5. java arryalist去重复_java ArrayList去重复值
  6. python timedelta_Python使用笔记:时间的运算timedelta
  7. android图片管理实例,Android图片处理实例介绍(图)
  8. mysql5.7.17启动失败_解决Mysql5.7.17在windows下安装启动时提示不成功问题
  9. Oracle JOB 用法小结
  10. MySQL 修改字段类型或长度