1053 Path of Equal Weight
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. 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, the number of nodes in a tree, M (<), the number of non-leaf nodes, and 0, the given weight number. The next line contains N positive numbers where Wi (<) corresponds to the tree node Ti. 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 { is said to be greater than sequence { if there exists 1 such that Ai=Bi for ,, and Ak+1>Bk+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
题意:
给出一棵树,每个节点既有编号又有权值,找出所有从根节点到叶子结点权值之和等于s的所有路径。
思路:
构造一个结构体,结构体中既有权重又有孩子结点,同时还有当前结点到达根节点的权重之和。其实寻找路径比较好找,我们用简单的DFS就能够把路径找到,但是问题要求输出的路径按照降序输出,看了别人的代码后,发现,在遍历孩子结点的时候如果我们能够保证孩子结点是由大到小的顺序,那么路径也就一定是由大到小的顺序。明白了只一点,这道题就简单了。
Code:
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 struct Node {
6 int val;
7 vector<int> sons;
8 int father = -1;
9 int sum = 0;
10 };
11
12 vector<int> leaf;
13 vector<Node> v;
14
15 bool cmp(int a, int b) { return v[a].val > v[b].val; }
16
17 void DFS(int index, int s) {
18 if (v[index].father != -1) {
19 int father = v[index].father;
20 v[index].sum = v[father].sum + v[index].val;
21 }
22 if (v[index].sons.size() == 0) {
23 if (v[index].sum == s) {
24 vector<int> path;
25 while (v[index].father != -1) {
26 path.push_back(v[index].val);
27 index = v[index].father;
28 }
29 path.push_back(v[index].val);
30 reverse(path.begin(), path.end());
31 for (int i = 0; i < path.size(); ++i) {
32 if (i == 0)
33 cout << path[i];
34 else
35 cout << " " << path[i];
36 }
37 cout << endl;
38 }
39 return;
40 }
41 for (int son : v[index].sons) DFS(son, s);
42 }
43
44 int main() {
45 int n, m;
46 long long s;
47 cin >> n >> m >> s;
48 v.resize(n);
49 for (int i = 0; i < n; ++i) {
50 cin >> v[i].val;
51 v[i].sum = v[i].val;
52 }
53 for (int i = 0; i < m; ++i) {
54 int index, k, son;
55 cin >> index >> k;
56 v[index].sum = v[index].val;
57 for (int j = 0; j < k; ++j) {
58 cin >> son;
59 v[index].sons.push_back(son);
60 v[son].father = index;
61 }
62 sort(v[index].sons.begin(), v[index].sons.end(), cmp);
63 }
64 DFS(0, s);
65 return 0;
66 }
1053 Path of Equal Weight的更多相关文章
- 【PAT】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 t ...
- PAT 1053 Path of Equal Weight[比较]
1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight Wi assigned t ...
- pat 甲级 1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT 甲级 1053 Path of Equal Weight (30 分)(dfs,vector内元素排序,有一小坑点)
1053 Path of Equal Weight (30 分) Given a non-empty tree with root R, and with weight Wi assigne ...
- 1053 Path of Equal Weight——PAT甲级真题
1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...
- 1053. Path of Equal Weight (30)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight of ...
- 1053 Path of Equal Weight (30 分)
Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weig ...
- PAT 1053 Path of Equal Weight
#include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> ...
- 1053 Path of Equal Weight (30)(30 分)
Given a non-empty tree with root R, and with weight W~i~ assigned to each tree node T~i~. The weight ...
- PAT Advanced 1053 Path of Equal Weight (30) [树的遍历]
题目 Given a non-empty tree with root R, and with weight Wi assigned to each tree node Ti. The weight ...
随机推荐
- 分布式流转开发常见报错FAQ
鸿蒙入门指南,小白速来!0基础学习路线分享,高效学习方法,重点答疑解惑--->[课程入口] HarmonyOS开发中分布式协同是非常重要的一个功能,大家在刚接触的时候可能会出现各种各样的错误.我 ...
- Python2和Python3编码的区别
Python2 python2中有两种储存变量的形式,第一种:Unicode:第二种:按照coding头来的. 假设python2用utf8存储x='中文',当你print(x)的时候,终端接收gbk ...
- 31Si2CrMoB
转: 31Si2CrMoB 31Si2CrMoB是推土机用钢的一种耐磨钢:此钢有很高的强度和韧度,适合于推土机XX.31Si2CrMoB冶炼技术工艺:电弧炉冶炼,初轧开坯.钢板轧制:可√189-170 ...
- redis基础:redis下载安装与配置,redis数据类型使用,redis常用指令,jedis使用,RDB和AOF持久化
知识点梳理 课堂讲义 课程计划 1. REDIS 入 门 (了解) (操作) 2. 数据类型 (重点) (操作) (理解) 3. 常用指令 (操作) 4. Jedis (重点) (操作) ...
- dom_bom学习
1. bom是什么? browser object model(浏览器对象模型) 在浏览器中 window指的就是bom对象 //网页重定向 // window.location.href=" ...
- 设计模式之工厂方法模式(Factory Method Pattern)
一.工厂方法模式的诞生 在读这篇文章之前,我先推荐大家读<设计模式之简单工厂模式(Simple Factory Pattern)>这篇文档.工厂方法模式是针对简单工厂模式中违反开闭原则的不 ...
- c++随机数问题研究
1.问题背景 某项目中有个复杂的排序,先是各种规则依次排序,最后如果依然并列的话,那就随机位置,名次并列.测试中发现一个诡异现象,并列时随机排序但随机后2个case打印的顺序每次都一样,随机数没有起到 ...
- MySQL全面瓦解25:构建高性能索引(案例分析篇)
回顾一下上面几篇索引相关的文章: MySQL全面瓦解22:索引的介绍和原理分析 MySQL全面瓦解23:MySQL索引实现和使用 MySQL全面瓦解24:构建高性能索引(策略篇) 索引的十大原则 1. ...
- WPF 基础 - Binding 的源与路径
1. 源与路径 把控件作为 binding 源与 binding 标记拓展: 控制 Binding 的方向及数据更新: Binding 的路径 Path: 没有路径的 Binding: 为 Bindi ...
- Cloudam云端,探索高性能计算在药物研究领域的解决方案
近日,Cloudam云端与国内某知名药企与合作,通过接入Cloudam云端自主研发的云E云超算服务,计算效率提高的数百倍.这也是云算力在生命科学领域的又一次成功应用.Cloudam云端云E云超算服务是 ...