Source:

PAT A1053 Path of Equal Weight (30 分)

Description:

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 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 W​i​​ (<) 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 { is said to be greater than sequence { if there exists 1 such that A​i​​=B​i​​ for ,, 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

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的更多相关文章

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

  2. 【PAT】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 ...

  3. PAT 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 t ...

  4. pat1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue G ...

  5. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  6. 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 W​i​​ assigne ...

  7. 1053 Path of Equal Weight——PAT甲级真题

    1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...

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

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

随机推荐

  1. python的代码块缓存机制,小数据池机制。

    同一代码块的缓存机制 在python中一个模块,一个函数,一个类,一个文件等都是一个代码块. 机制内容:Python在执行同一个代码块的初始化对象的命令时,会检查是否其值是否已经存在,如果存在,会将其 ...

  2. docker哪些平台技术(3)

    容器平台技术 容器核心技术使得容器能够在单个 host 上运行.而容器平台技术能够让容器作为集群在分布式环境中运行. 容器平台技术包括容器编排引擎.容器管理平台和基于容器的 PaaS. 容器编排引擎 ...

  3. STM32嵌入式开发学习笔记(一)

    本文中,笔者将介绍使用嵌入式开发工具Keil uVision5,使用C语言,对微处理器STM32F103C8进行嵌入式开发. 开发使用C语言,首先需要新建一个C语言文件,将其设为主函数的入口,因此,将 ...

  4. NYOJ 737 (石子合并)

    该题是一道DP题,核心思想如下: 某个区间一定是这个区间内的某两个子区间合成的(这两个子区间互补,即这两个区间加起来等于大区间), 所以我们枚举所有的情况,取个最大值即可.因为最初是从2堆石子开始无法 ...

  5. 二:unittest框架配合selenium之xpath定位

    刚开始学习selenium自动化测试时,犯了一个不该犯的错误,偷懒,使用火狐浏览器中的扩展FIREBUG,FIREPATH来辅助定位. 虽然用的定位方法大多数是使用XPATH方法,但是是工具定位出来的 ...

  6. PHP面试 PHP基础知识 二(常量及数据类型)

    常量及数据类型 PHP八种数据类型 四种标量类型 字符串(string).布尔(boolean).浮点(float/double).整型(integer) 两种复合类型 数组(array).对象(ob ...

  7. Java异常关闭资源的两种方式

    try-catch-finally 常用,在异常关闭时应判断流是否为空 public class CloseableUtils { public static void closeable(Close ...

  8. Java方法中形参能否改变实参

    前几天学习了java中的参数传递机制,总结了一些知识点: 1·参数类型为基本数据类型:整型:byte,short,int,long ,浮点型:float,double ,字符型:char ,布尔型:b ...

  9. JOGL图形形状

    图形对象 要访问程序特定于硬件和操作系统平台,以及其他语言编写,比如C和C++(原生应用)库,Java使用一种称为Java本地接口(JNI)编程框架的工作. JOGL内部使用此接口,如图中下面的图表来 ...

  10. CSS:Stacking Context

    通常情况下,HTML页面可以被认为是二维的,因为文本,图像和其他元素被排列在页面上而不重叠.在这种情况下,只有一个渲染进程,所有元素都知道其他元素所占用的空间.z-index属性可让你在渲染内容时调整 ...