PAT甲级——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 a path from R to L is defined to be the sum of the weights of all the nodes along the path from R to 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-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 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
深度遍历
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Node
{
int val;
vector<int>child;
}node[];
int N, M, S;
int path[];
void DFS(int head, int numNode, int sum)
{
if (sum > S)
return;
if (sum == S)
{
if (node[head].child.size() != )//不是叶子节点
return;
for (int i = ; i < numNode; ++i)
cout << node[path[i]].val << (i < numNode - ? " " : "");
cout << endl;
return;
}
for (int i = ; i < node[head].child.size(); ++i)
{
path[numNode] = node[head].child[i];
DFS(node[head].child[i], numNode + , sum + node[node[head].child[i]].val);
}
}
int main()
{
cin >> N >> M >> S;
for (int i = ; i < N; ++i)
cin >> node[i].val;
int a, b, k;
for (int i = ; i < M; ++i)
{
cin >> a >> k;
for (int j = ; j < k; ++j)
{
cin >> b;
node[a].child.push_back(b);
}
sort(node[a].child.begin(), node[a].child.end(),
[](int a, int b) {return node[a].val > node[b].val; });
}
path[] = ;
DFS(, , node[].val);
return ;
}
PAT甲级——A1053 Path of Equal Weight的更多相关文章
- 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 ...
- 【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 ...
- 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 ...
- PAT Advanced 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 ...
- PAT_A1053#Path of Equal Weight
Source: PAT A1053 Path of Equal Weight (30 分) Description: Given a non-empty tree with root R, and w ...
- 1053 Path of Equal Weight——PAT甲级真题
1053 Path of Equal Weight 给定一个非空的树,树根为 RR. 树中每个节点 TiTi 的权重为 WiWi. 从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 L ...
- 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 ...
- 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 ...
随机推荐
- 在select标签中添加a标签
<!--第一个选项不能写连接--> <select id="" onchange="window.location=this.value"&g ...
- mysql重点,表查询操作和多表查询
表单查询 1. 完整的查询语句语法 select distinct(* or 字段名 or 四则运算 )from 表名 where 条件 group by 条件 having 条件 order by ...
- Jenkins 自动部署
一.安装插件[系统管理 → 插件管理 ] 为了通过SSH上传war包,我们需要安装Publish Over SSH 插件. 二.添加SSH 服务器[系统管理→系统设置] 参数说明: Name:ss ...
- day 65 Django基础之django分页
Django基础之django分页 一.Django的内置分页器(paginator) view from django.shortcuts import render,HttpRespons ...
- javaweb中静态文件的处理方法
方案一:激活Tomcat的defaultServlet来处理静态文件 在web.xml中添加: <servlet-mapping> <servlet-name>default& ...
- JS规则 多样化的我(变量赋值)我们使用"="号给变量存储内容,你可以把任何东西存储在变量里,如数值、字符串、布尔值等,
多样化的我(变量赋值) 我们可以把变量看做一个盒子,盒子用来存放物品,那如何在变量中存储内容呢? 我们使用"="号给变量存储内容,看下面的语句: var mynum = 5 ; / ...
- Luogu P4246 [SHOI2008]堵塞的交通(线段树+模拟)
P4246 [SHOI2008]堵塞的交通 题意 题目描述 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常奇特,整个国家的交通系统可以被看成是一个\(2\)行\(C\)列的矩形 ...
- 第三方下载控件 用起来还是不错的偶!Aria
本文主要介绍开源项目Aria的使用. 先在项目里的build 中配置compile 'com.arialyy.aria:Aria:3.1.1' //下载 开始下载 Aria.download(this ...
- Django之ORM查询优化
目录 only 和 defer select_related 和 prefetch_related ORM字段参数 choices res = models.Book.objects.all() # ...
- 拓扑排序+并查集——cf1131D
以前做过了忘记掉了..拓扑排序如果要处理等于关系,就要用并查集把相等关系进行缩点 /* 1.相等关系用并查集合并 2.不等关系用有向边链接 3.拓扑排序求顺序 */ #include<bits/ ...