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
IDis a two-digit number representing a given non-leaf node,Kis 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 ...
随机推荐
- 区间dp+预处理——cf1278F(难题)
感觉很难的区间dp,主要是状态难想 /* 对于一个区间[i,j],设其最小的颜色编号是c=Min[i,j],那么该区间显然有一大段是以c为底的 设这个颜色在该区间出现位置的两端是L[c],R[c],那 ...
- Python每日一题 001
Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...
- git stash 保存和恢复进度
1. stash当前修改 git stash会把所有未提交的修改(包括暂存的和非暂存的)都保存起来,用于后续恢复当前工作目录. 比如下面的中间状态,通过git stash命令推送一个新的储藏,当前的工 ...
- BZOJ 1565: [NOI2009]植物大战僵尸(网络流+缩点)
传送门 解题思路 最大权闭合子图.但是要注意一些细节,假如有一堆植物形成一个环,那么这些植物都是无敌的,并且他们保护的植物是无敌的,他们保护的保护的植物是无敌 的.所以要缩点,然后拓扑排序一次判无敌, ...
- 最全的PS快捷键大全!
一.工具箱 01.(多种工具共用一个快捷键的可同时按[Shift]加此快捷键选取)02.矩形.椭圆选框工具 [M]03.裁剪工具[C]04.移动工具[V]05.套索.多边形套索.磁性套索[L]06.魔 ...
- CentOS7简单安装mplayer和vlc!
http://pkgs.org/在这个网上搜索下面的包的最新版1. sudo rpm -ivh epel-release-7-0.2.noarch.rpm 2. sudo rpm -Uvh elrep ...
- (转)OpenFire源码学习之六:用户注册
转:http://blog.csdn.net/huwenfeng_2011/article/details/43413509 用户注册 注册流程: 1.客户端进行握手给服务端发送连接消息: <s ...
- LInux文件基础知识和文件目录操作(系统调用函数方式)
1.进程是处于活动状态的程序,某个用户通过操作系统运行程序所产生的进程代表着该用户的行为.如果用户不具备访问某个目录和文件的权限,那么该用户的进程也不能访问. 2.Linux系统中文件安全机制是通过给 ...
- js实现点击按钮传值
js实现点击按钮传值 page1源码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8&quo ...
- 6、通过Appium Desktop 实现录制功能
1.老规矩,我们进入下面这个界面 图中红色标记1为 “top by coordinates” 按钮, 这是一种通过坐标定位元素的方式. 图中红色标记2为 “Start Recording” 按钮, ...