原文链接:http://kakazai.cn/index.php/Kaka/Pat/query/id/143
题目
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805424153280512
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<N≤100, the number of nodes in a tree, M (<N), the number of non-leaf nodes, and 0<S<2^30, the given weight number. The next line contains N positive numbers where Wi (<1000) 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 {A1,A2,⋯,An} is said to be greater than sequence {B1,B2,⋯,Bm} if there exists 1≤k<min{n,m}such that Ai=Bi for i=1,⋯,k, 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
题意分析
- 题意
给定一棵树,每个结点都有权重。现在求出从根结点出发到叶结点的一条路径,该路径的权重是各个结点的权重总和,要求该路径的权重等于给定的值。如果有多条这样的路径,则要先输出子序列大的路径。比如路径[10 5 2 7],比路径[10 4 10]的子序列要大。
- 分析
用深度遍历出每一条从根结点出发到叶子结点的路径,若该路径权重符合要求,则输出。注意,每个结点的孩子结点都要按权重降序排列,这样在遍历的时候,会先遍历到权重大的孩子结点,从而保证先输出的路径必然是子序列最大的。
知识点与坑点
- 知识点
1)DFS,子序列
- 坑点
1)
一、DFS
算法思路
1 存储好所有结点的权重,所有结点的孩子结点,并将孩子结点按权重降序排列
2 用深度遍历,从根结点出发,探索出所有到达叶子结点的路径
3 输出总权重符合要求的路径
代码-c++版
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 101; //最多有100个结点
int n, en, cmax; //点数、边数、给定权重总和
vector<int> no[maxn]; //点的孩子
int w[maxn]; //点的权重
/* 打印给定路径*/
void print(vector<int> curpath){
printf("%d", curpath[0]);
for (int i = 1; i < curpath.size(); i++) {
printf(" %d", curpath[i]);
}
printf("\n");
return;
}
/*用DFS遍历所有可能的路径,并打印符合条件的路径*/
vector<int> curpath; //存储当前路径
void dfs(int cur, int curpw) { //cur是当前结点,curpw是当前路径的总权重
/*当前结点是叶结点*/
if (no[cur].size() == 0) {
if (curpw == cmax) { //总权重符合要求
print(curpath);//打印当前路径
}
return;
}
/*当前结点是非叶结点*/
for (int i = 0; i < no[cur].size(); i++) {
int id = no[cur][i];
//探索从该孩子id出发的路径
curpath.push_back(w[id]); //孩子id权重入栈
dfs(id, curpw + w[id]);
//探索完后,该孩子id出栈,以便探索从其他孩子出发的路径
curpath.pop_back();
}
}
/*将点按权重降序*/
int cmp(int a, int b) {
return w[a] > w[b];
}
int main() {
scanf("%d %d %d", &n, &en, &cmax);
/*接收结点的权重*/
for (int i = 0; i < n; i++) {
scanf("%d", &w[i]);
}
/*接收结点的所有孩子,并根据权重对孩子降序排列*/
int id, k, in1;
for (int i = 0; i < en; i++) {
scanf("%d %d", &id,&k);
for (int j = 0; j < k; j++) {
scanf("%d", &in1);
no[id].push_back(in1);
}
sort(no[id].begin(), no[id].end(), cmp);
}
/*从根结点开始,深度遍历所有可能的路径*/
curpath.push_back(w[0]);//根结点权重入栈
dfs(0,w[0]);
return 0;
}
代码-python版