1053 Path of Equal Weight——PAT甲级真题
1053 Path of Equal Weight
给定一个非空的树,树根为 RR。
树中每个节点 TiTi 的权重为 WiWi。
从 RR 到 LL 的路径权重定义为从根节点 RR 到任何叶节点 LL 的路径中包含的所有节点的权重之和。
现在给定一个加权树以及一个给定权重数字,请你找出树中所有的权重等于该数字的路径(必须从根节点到叶节点)。
例如,我们考虑下图的树,对于每个节点,上方的数字是节点
ID,它是两位数字,而下方的数字是该节点的权重。假设给定数为 2424,则存在 44 个具有相同给定权重的不同路径:
{10 5 2 7},{10 4 10},{10 3 3 6 2},{10 3 3 6 2}, 已经在图中用红色标出。
输入格式
第一行包含三个整数 N,M,SN,M,S,分别表示树的总节点数量,非叶子节点数量,给定权重数字。
第二行包含 NN 个整数 WiWi,表示每个节点的权重。
接下来 MM 行,每行的格式为:
ID K ID[1] ID[2] ... ID[K]
IDID 是一个两位数字,表示一个非叶子结点编号,KK 是一个整数,表示它的子结点数,接下来的 KK 个 ID[i]ID[i] 也是两位数字,表示一个子结点的编号。
出于方便考虑,根节点固定为 0000,且树中所有节点的编号为 00∼N−100∼N−1。
输出格式
以单调递减的顺序输出所有权重为S的路径。
每个路径占一行,从根节点到叶节点按顺序输出每个节点的权重。
注意:我们称 AA 序列 {A1,A2,…,An}{A1,A2,…,An} 大于 BB 序列 {B1,B2,…,Bm}{B1,B2,…,Bm},当且仅当存在一个整数 kk,1≤k<min(n,m)1≤k<min(n,m),对于所有 1≤i≤k1≤i≤k,Ai=BiAi=Bi 成立,并且 Ak+1>Bk+1Ak+1>Bk+1。
数据范围
1≤N≤1001≤N≤100,
0≤M<N0≤M<N,
0<S<2300<S<230,
0<Wi<10000<Wi<1000输入样例:
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
输出样例:
10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2
题目大意:给你一棵树,让你从叶节点到根节点的权值之和等于S的路径。
大致思路:题目要求我们输出的时候要按照结点权值由大到小输出,所以在读入每个节点的孩子结点的时候,把每个结点的孩子结点按照权值的大小由大到小排序。同时我们还要定义一个path数组用来记录搜索路径。最后进行DFS。
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
struct node {
long long val; //每个结点的权重
vector<int> child;
long long weight; //记录每个结点的权重
} root[N];
long long n, m, s;
vector<int> path;
bool vis[N]; //标记当前结点有没有访问过
int cnt = 0;
void DFS(int x) {
if (root[x].child.size() == 0) {
if (root[x].weight == s) {
// cout << "叶子结点为:" << x << endl;
// cout << "结点权值是:" << root[x].weight << endl;
for (int i = 0; i < path.size(); i++) {
cout << root[path[i]].val;
if (i != path.size() - 1)
cout << " ";
else
cout << endl;
}
return;
}
return;
}
for (int i = 0; i < root[x].child.size(); i++) {
int tmp = root[x].child[i];
int w = root[tmp].weight;
if (!vis[tmp]) {
vis[tmp] = true;
root[tmp].weight += root[x].weight;
path.push_back(tmp);
DFS(tmp);
vis[tmp] = false;
root[tmp].weight = w; //回溯
path.pop_back();
}
}
}
bool cmp(int a, int b) { return root[a].weight > root[b].weight; }
int main() {
memset(vis, 0, sizeof(vis));
cin >> n >> m >> s;
for (int i = 0; i < n; i++) {
scanf("%lld", &root[i].val);
root[i].weight = root[i].val;
}
for (int i = 0; i < m; i++) {
int id, k;
cin >> id >> k;
for (int j = 0; j < k; j++) {
int x;
cin >> x;
root[id].child.push_back(x);
}
sort(root[id].child.begin(), root[id].child.end(), cmp);
}
path.push_back(0);
DFS(0);
return 0;
}
1053 Path of Equal Weight——PAT甲级真题的更多相关文章
- 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 ...
- 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 ...
- PAT 甲级真题题解(1-62)
准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format 模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...
- 1080 Graduate Admission——PAT甲级真题
1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...
- 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)
题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...
- PAT 1053 Path of Equal Weight
#include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> ...
- 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 ...
随机推荐
- VMware vCenter 6.0 安装及群集配置介绍(转载)
转载自http://blog.51cto.com/wzlinux/2094598 一.介绍 VMware vCenter Server 提供了一个可伸缩.可扩展的平台,为虚拟化管理奠定了基础.可集中管 ...
- 一块网卡配2IP地址
我们知道在Linux下网卡被称为eth0,eth1,eth2.....,所有网卡的配置文件都存储在 /etc/sysconfig/network-script/下,文件名是以ifcfg-eth0,if ...
- Flink-v1.12官方网站翻译-P021-State & Fault Tolerance-overview
状态和容错 在本节中,您将了解Flink为编写有状态程序提供的API.请看一下Stateful Stream Processing来了解有状态流处理背后的概念. 下一步去哪里? Working wit ...
- c++nullptr(空指针常量)、constexpr(常量表达式)
总述 又来更新了,今天带来的是nullptr空指针常量.constexpr(常量表达式)C++的两个用法.Result result_fun = nullptr;constexpr stati ...
- CF-1445 C - Division 数论,质因数,唯一分解定理
题意 给定一个 \(p (p\le 10^{18})\), 一个 \(q(q \le 10^9)\), 要找到一个最大的 \(x\) 满足: \(p \%x = 0\) \(q \% x \neq 0 ...
- 2019 ccpc秦皇岛
1006 (dfs) #include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const dou ...
- UVA - 12295 最短路(迪杰斯特拉)——求按对称路线最短路条数
题意: 给你一个n,然后给你一个n*n的正方形w[i][j],你需要找到一个从(1,1)点走到(n,n)点的最短路径数量.而且这个路径必须按照y=x对称 题解: 我们把左上角的点当作(0,0)点,右下 ...
- Atcoder Beginner Contest 168 D - .. (Double Dots) (BFS)
题意:有\(n\)个房间,在这些房间中两两连\(m\)次条边,问除了第一个房间,其他房间走到第一个房间的最短路径,输出这个房间所连的上一个房间,如果走不到,输出\(no\). 题解:刚开始我写了一个d ...
- Kubernets二进制安装(10)之部署主控节点部署调度器服务kube-scheduler
Kubernetes Scheduler是一个策略丰富.拓扑感知.工作负载特定的功能,调度器显著影响可用性.性能和容量.调度器需要考虑个人和集体的资源要求.服务质量要求.硬件/软件/政策约束.亲和力和 ...
- codeforce 849B
B. Tell Your World time limit per test 1 second memory limit per test 256 megabytes input standard i ...
