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 ...
随机推荐
- vb.net和C#两兄弟谁的封装性更好?
引言: 虽然没怎样接触过vb.net,但是大概对于vb6还是比较了解的.前者是从后者基础上发展而来的.后来接触了C#编程语言,起初没有太关心她和vb.net有啥不同的地方,话说都是面向对象的,而且都是 ...
- 深入理解nodejs的HTTP处理流程
目录 简介 使用nodejs创建HTTP服务 解构request 处理Request Body 处理异常 解构response 简介 我们已经知道如何使用nodejs搭建一个HTTP服务,今天我们会详 ...
- docker(12)使用Dockerfile创建jenkins+python3+pytest环境
前言 之前我们用docker手动安装了jenkins环境,在jenkins中又安装了python3环境和各种安装包,如果我们想要在其他3台机器上安装,又是重复操作,重复劳动,那会显得很low,这里可以 ...
- ElasticSearch结合Logstash(三)
一.Logstash简介 1,什么是Logstash Logstash 是开源的服务器端数据处理管道,能够同时从多个来源采集数据,转换数据,然后将数据发送到您最喜欢的"存储库"中. ...
- hdu1313 Round and Round We Go (大数乘法)
Problem Description A cyclic number is an integer n digits in length which, when multiplied by any i ...
- Codeforces Round #613 (Div. 2) C. Fadi and LCM (数学)
题意:给你一个正整数\(x\),找两个正整数\(a\),\(b\),使得\(lcm(a,b)=x\),并且\(max(a,b)\)最小. 题解:我们知道,\(lcm(a,b)=a*b/gcd(a,b) ...
- 网易云音乐JS逆向解析歌曲链接
Request URL: https://music.163.com/weapi/song/enhance/player/url?csrf_token= FormData : params: BV ...
- kubernetes实战-配置中心(二)交付apollo配置中心到k8s
apollo官网:官方地址 apollo架构图: apollo需要使用数据库,这里使用mysql,注意版本需要在5.6以上: 本次环境mysql部署在10.4.7.11上,使用mariadb:10.1 ...
- github 无法访问
描述: 1. ping 丢失 100% 2. git 失败 Failed to connect to github.com port 443: Timed out 3.打开网站 超时 解决: http ...
- ++i和i++的区别
它们两个的数值变化的区别,我这里就不多说了 这里主要说明两者在效率上的区别 (1)首先如果是自带的数据类型,比如int型,++i和i++,编译器的实现方式是相同的,两者并没有效率上的区别,虽然也有副本 ...