PAT_A1053#Path of Equal Weight
Source:
Description:
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 Rto 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-digitID
's of its children. For the sake of simplicity, let us fix the root ID to be00
.
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
Keys:
Code:
/*
time: 2019-06-28 16:28:41
problem: PAT_A1053#Path of Equal Weight
AC: 21:32 题目大意:
树的带权路径长度:根结点到各个叶子结点带权路径长度之和
现给定带权路径长度,打印所有等于该长度的路径 输入:
第一行给出:结点数N<100,分支结点数M,给定带权路径长度S
接下来一行,N个结点的权值(0~n-1)
接下来M行,分支结点id,孩子数K,各孩子id(根结点00)
输出:
各路径按照权值非增打印 基本思路:
递减存储各分支结点的孩子结点,遍历并统计带权路径,符合条件的输出之
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=;
vector<int> tree[M],path;
int w[M],pt=,s; bool cmp(int a, int b)
{
return w[a] > w[b];
} void Travel(int root, int value)
{
if(tree[root].size()== && value==s)
{
path.push_back(root);
for(int i=; i<path.size(); i++)
printf("%d%c", w[path[i]],i==path.size()-?'\n':' ');
path.pop_back();
return;
}
path.push_back(root);
for(int i=; i<tree[root].size(); i++)
Travel(tree[root][i],value+w[tree[root][i]]);
path.pop_back();
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,m;
scanf("%d%d%d", &n,&m,&s);
for(int i=; i<n; i++)
scanf("%d", &w[i]);
for(int i=; i<m; i++)
{
int id,k,kid;
scanf("%d%d", &id, &k);
for(int j=; j<k; j++)
{
scanf("%d", &kid);
tree[id].push_back(kid);
}
sort(tree[id].begin(), tree[id].end(), cmp);
}
Travel(,w[]); return ;
}
PAT_A1053#Path of Equal Weight的更多相关文章
- Path of Equal Weight (DFS)
Path of Equal Weight (DFS) Given a non-empty tree with root R, and with weight Wi assigned to each ...
- 【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 ...
- pat1053. Path of Equal Weight (30)
1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...
- 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 ...
- A1053. 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 ...
随机推荐
- kubernetes(k8s)集群安全机制RBAC
1.基本概念 RBAC(Role-Based Access Control,基于角色的访问控制)在k8s v1.5中引入,在v1.6版本时升级为Beta版本,并成为kubeadm安装方式下的默认选项, ...
- 堆、栈、方法区、静态代码块---Java
java 堆.栈.方法区 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指令) 2.jvm只有一个堆区(heap)被所有线程共享,堆中不存放基 ...
- 如何在Python中让两个print()函数的输出打印在一行内?
1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用‘,’代替空格,且默认末尾加 ...
- UVA - 10347 - Medians(由三中线求三角形面积)
AC代码: #include<cstdio> #include<cmath> #include<algorithm> #include<iostream> ...
- CTF杂项思路工具分享————2019/5/30
分享碰到的一些奇奇怪怪的杂项解题方式: 键盘坐标密码: 题目给出一段字符串:11 21 31 18 27 33 34 对照上面的表格,就可以很清晰的看出来密文为:QAZIJCV 猪圈码: 题目为: 一 ...
- 68.最大k乘积问题 (15分)
C时间限制:3000 毫秒 | C内存限制:3000 Kb题目内容:设I是一个n位十进制整数.如果将I划分为k段,则可得到k个整数.这k个整数的乘积称为I的一个k乘积.试设计一个算法,对于给定的I和 ...
- 7-vim-移动命令-02-行数跳转和上下翻页
1.行数跳转 命令 英文 功能 gg go 文件顶部 G GO 文件尾部 数字gg 移动到数字对应行数 数字G 移动到数字对应行数 :数字 移动到数字对应行数 2.屏幕移动 命令 英文 功 ...
- extern static和函数
#include <stdio.h> int sum(int a, int b); int main() { /************************************** ...
- CompletionService:批量执行异步任务
之前有去了解CompletionService,当时没有做记录,现在再想使用却发现忘记了,果然好记性不如烂笔头. 原文比较简洁,通俗易懂. 转载-原文链接: https://segmentfault. ...
- HashMap和Hashtable有什么区别
HashMap和Hashtable都实现了Map接口,因此很多特性非常相似.但是,他们有以下不同点: HashMap允许键和值是null,而Hashtable不允许键或者值是null. Hashtab ...