✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:PAT题解集合
📝原题地址:题目详情 - 1053 Path of Equal Weight (pintia.cn)
🔑中文翻译:等重路径
📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪
1053 Path of Equal Weight
Given a non-empty tree with root R R R, and with weight W i W_i Wi assigned to each tree node T i T_i Ti. The weight of a path from R R R to L L L is defined to be the sum of the weights of all the nodes along the path from R R R to any leaf node L L 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.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hZ96rv96-1664980439849)(PAT 甲级辅导.assets/212)]
Input Specification:
Each input file contains one test case. Each case starts with a line containing 0< N N N≤100, the number of nodes in a tree, M M M (< N N N), the number of non-leaf nodes, and 0< S S S<230, the given weight number. The next line contains N N N positive numbers where W i W_i Wi (<1000) corresponds to the tree node T i T_i Ti. Then M M 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-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 { A 1 , A 2 , ⋯ , A n A_1,A_2,⋯,A_n A1,A2,⋯,An} is said to be greater than sequence { B 1 , B 2 , ⋯ , B m B_1,B_2,⋯,B_m B1,B2,⋯,Bm} if there exists 1 ≤ k < m i n { n , m } 1≤k<min\{n,m\} 1≤k<min{n,m} such that A i = B i A_i=B_i Ai=Bi for i=1,⋯,k, and A k + 1 > B k + 1 A_{k+1}>B_{k+1} 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
题意
给定一个值 S S S ,要求在树中从根结点到叶子结点的所有路径中找到路径权值之和等于 S S S 的路径。
假设给定数为 24
,则存在 4
个具有相同给定权重的不同路径:{10 5 2 7},{10 4 10},{10 3 3 6 2},{10 3 3 6 2}
, 已经在图中用红色标出。
第一行包含三个整数 N , M , S N,M,S N,M,S ,分别表示树的总节点数量,非叶子节点数量,给定权重数字。
第二行包含 N N N 个整数 W i W_i Wi,表示每个节点的权重。
接下来 M M M 行,每行的格式为:
ID K ID[1] ID[2] ... ID[K]
ID
是一个两位数字,表示一个非叶子结点编号,
K
K
K 是一个整数,表示它的子结点数,接下来的
K
K
K 个 ID[i]
也是两位数字,表示一个子结点的编号。
出于方便考虑,根节点固定为 00
,且树中所有节点的编号为 00∼N−1
。
输出以单调递减的顺序输出所有权重为 S S S 的路径。
注意:这里单调递减指的是结果路径中按照字典序单调递减,比如 {4,1,5,3}
要比 {4,0,5,3}
和 {4,0,5,3,4}
都要大。
思路
- 利用邻接矩阵来存储每个结点之间的关系。
- 通过递归的方式寻找路径:
- 首先要判断当前结点是否为叶子结点。
- 如果是叶子结点则再判断当前路径之和是否等于
S
S
S ,如果等于则将当前路径加入答案数组
ans
中。 - 如果不是叶子结点则需要继续遍历其所有的孩子结点。
- 利用
sort
函数自带的排序性质即会自动按照字典序进行排序,只需要加一个greater<vector<int>>()
的标志,表示以降序的方式排序。 - 输出最终结果,注意每行末尾不能有空格。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int n, m, S;
int w[N];
bool g[N][N];
vector<vector<int>> ans;
void dfs(int u, int s, vector<int>& path)
{
//判断当前结点是否为叶子结点
bool is_leaf = true;
for (int i = 0; i < n; i++)
if (g[u][i])
{
is_leaf = false;
break;
}
if (is_leaf) //如果是叶子结点
{
if (s == S) ans.push_back(path);
}
else //如果不是叶子结点
{
for (int i = 0; i < n; i++)
{
if (!g[u][i]) continue;
path.push_back(w[i]);
dfs(i, s + w[i], path);
path.pop_back();
}
}
}
int main()
{
cin >> n >> m >> S;
for (int i = 0; i < n; i++) cin >> w[i]; //输入每个结点的权值
for (int i = 0; i < m; i++) //输入所有结点的信息
{
int id, k;
cin >> id >> k;
while (k--)
{
int son;
cin >> son;
g[id][son] = true;
}
}
vector<int> path = { w[0] };
dfs(0, w[0], path);
//sort会自动按照字典序的方式进行排序
sort(ans.begin(), ans.end(), greater<vector<int>>());
for (auto p : ans)
{
cout << p[0];
for (int i = 1; i < p.size(); i++) cout << " " << p[i];
cout << endl;
}
return 0;
}