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. [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)

    == 0) { convertView.setBackgroundResource(R.drawable.list_gray_9); } else { convertView.setBackgroun ...

  2. Luogu P2042 [NOI2005]维护数列

    题目描述 请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线' _ '表示实际输入文件中的空格) 输入输出格式 输入格式: 输入文件的第 1 行包含两个数 N 和 M, ...

  3. Python 生成json文件

    1.数据准备 数据下载 2.python代码 import datetime import os import mssqlhelper ms = mssqlhelper.MSSQL(host=&quo ...

  4. centos coreseek

    下载稳定版 coreseek wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz 解压 .tar.gz cd cor ...

  5. vim编辑器设置缩进!

    转载自 http://blog.chinaunix.net/uid-27213819-id-3813909.html 1.在自己的home目录下建立.vimrc文件.控制台输入vi ~/.vimrc ...

  6. ASP.NET Core学习——前言

    跌跌撞撞,公司的新项目终于要在这个月月底上线. 新项目使用ASP.NET Core来做,以前没接触过这方面的内容,只能一边学习,一边搞开发. 眼看项目上线在即,工作没那么忙,也不需要天天加班. 回想了 ...

  7. URAL 1748 The Most Complex Number

    题目链接:https://vjudge.net/problem/11177 题目大意: 求小于等于 n 的最大反素数. 分析: n <= 10^18,而前20个素数的乘积早超过10^18,因此可 ...

  8. 『Golang』—— 标准库之 time

    ... package main import ( "fmt" "time" ) func main() { time.AfterFunc(time.Milli ...

  9. Java.util.ArrayDeque类

    java.util.ArrayDeque 类提供了可调整大小的阵列,并实现了Deque接口.以下是关于阵列双端队列的要点: 数组双端队列没有容量限制,使他们增长为必要支持使用. 它们不是线程安全的;如 ...

  10. BigDecimal的操作工具类

    import java.math.BigDecimal; /** * 进行BigDecimal对象的加减乘除,四舍五入等运算的工具类 * @author ameyume * */ public cla ...