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甲级真题的更多相关文章

  1. pat 甲级 1053. Path of Equal Weight (30)

    1053. Path of Equal Weight (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. 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 ...

  3. 【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 ...

  4. 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 ...

  5. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  6. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  7. 【PAT甲级】1053 Path of Equal Weight (30 分)(DFS)

    题意: 输入三个正整数N,M,S(N<=100,M<N,S<=2^30)分别代表数的结点个数,非叶子结点个数和需要查询的值,接下来输入N个正整数(<1000)代表每个结点的权重 ...

  8. PAT 1053 Path of Equal Weight

    #include <cstdio> #include <cstdlib> #include <vector> #include <algorithm> ...

  9. 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 ...

随机推荐

  1. Spring Cloud与Docker——微服务架构概述

    Spring Cloud与Docker--微服务架构概述 单体应用架构概述 微服务概述 微服务的特性 微服务架构的优点 微服务面临的挑战 微服务的设计原则 单体应用架构概述 传统的服务发布都是采用单体 ...

  2. python模块----os模块 (操作系统接口模块)

    os模块提供一种使用与操作系统相关的功能的便捷式途径. 一定要使用 import os 而不是 from os import * .这将避免内建的 open() 函数被 os.open() 隐式替换掉 ...

  3. Spring boot 使用Redis 消息队列

    package com.loan.msg.config; import com.loan.msg.service.MessageReceiver; import org.springframework ...

  4. shell循环字符串数组

    #!/bin/bash arr=("0" "1" "2" "3" "4" "5" ...

  5. Docker --volume(数据持久化)

    数据卷 volume 数据卷 是一个可供一个或多个容器使用的特殊目录,实现让容器中的一个目录和宿主机中的一个文件或者目录进行绑定.数据卷 是被设计用来持久化数据的 第一种:bind mount vol ...

  6. CF-1328 F. Make k Equal

    F. Make k Equal 题目链接 题意 长度为n的序列,每次可以选择一个最大的数字将其减一或者选择一个最小的数字将其加一,问最少操作多少次可以使得序列中至少存在 k 个一样的数字 分析 官方题 ...

  7. CF 1288 E. Messenger Simulator

    CF 1288 E. Messenger Simulator 题目传送门 官方题解 题意想必大家都明白了这里就不赘述了,这里只想重点记录一下几种实现方法 分析 设向前移动的序列为\(a\)序列 对于没 ...

  8. 2019牛客多校 Round7

    Solved:5 Rank:296 E Find the median (线段树) 题意:最开始一个空的数组 4e5次操作 每次把Li,Ri中的每个数插入进来 问当前的中位数 题解:把这n个区间离散化 ...

  9. UVALive 7146

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

  10. Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! D. Johnny and Contribution (贪心,模拟)

    题意:有\(n\)个点,\(m\)条边,现在要给这些点赋值,,每次只能赋给某一点的四周(所连边)的最小没出现过的值.如果不能按照所给的数赋值,输出\(-1\),否则输出赋值顺序. 题解:我们用\(pa ...