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. 【SQL】事务回滚

    事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务,SQL Server能将逻辑相关的一组操作绑定在一起,以便服 ...

  2. Openstack组件部署 — Networking service_Compute Node

    目录 目录 前文列表 安装组件 配置通用组件 配置自服务网络选项 配置Linux 桥接代理 配置Nova使用网络 完成安装 验证操作Execute following commands on Cont ...

  3. hexo next博客之无敌之舒服之美妙之轻松之发布博客(mweb,github自主开发插件)

    文章目录 **前言:** 具体用法 **mweb for mac** 注意 **github插件下载** 个人博客:https://mmmmmm.me 源码:https://github.com/da ...

  4. 无法将 Ethernet0 连接到虚拟网络”VMnet0″ 详细信息可以在 vmware.log 文件中找到未能连接虚拟机Ethernet0

    在 vmware“编辑->虚拟网络设置”里面,点“恢复默认”可解决.  

  5. C++——指针与数组

    1.数组名不是指针,神似指针(可以将数组名直接赋值给指针) (1)数组名的内涵在于其指代实体是一种数据结构,这种数据结构就是数组:(2)数组名的外延在于其可以转换为指向其指代实体的指针,而且是一个指针 ...

  6. 拾遗:Git 常用操作回顾

    温故而知新,可以为师矣. Git 布局 工作区---->暂存区---->本地仓库---->远程仓库 Create Repository git init PATH git add P ...

  7. tensorflow 训练最后预测结果为一个定值,可能的原因

    训练一个分类网络,没想到预测结果为一个定值. 找了很久发现,是因为tensor的维度的原因.  注意:我说的是我的label数据的维度. 我的输入是: y_= tf.placeholder(tf.in ...

  8. 错误 1 error C4996: 'getcwd': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getcwd. See online help for details.

    解决办法: 属性>C/C++>预处理定义>编辑>添加_CRT_NONSTDC_NO_DEPRECATE>应用

  9. 结合Poi实现可读取Excel的文件选择对话框

    第一步:ApachePoi的jar包导全,不全会出现异常. 第二步:写就完事了:此例为读取特定模板的excel,仅供参考,根据实际需求改写. package 自建包; import java.awt. ...

  10. [已解决]windows安装docker的问题

    windows下载安装docker出现的问题 进入powershell后输入docker --version报错: could not read CA certificate "C:\\Us ...