【原题目】
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.
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
《算法笔记》无法通过最后一个测试点的原因:在于 sort 的排序方法上有问题
此解法在读入时事先对每个非叶子结点的子结点进行排序,即对 vector 数组中的子结点编号按照该子结点对应的权值从大到小进行排序;只能保证该父节点下的直接儿子结点是从大到小的顺序,但若直接儿子结点相同,则这些直接儿子下的孙子结点并不会按照从大到小的顺序输出路径!
举个例子,假设输入如下:
7 5 23
10 5 5 3 7 7 3
00 2 01 02
01 1 03
02 1 04
03 1 05
04 1 06
根据输入画出的树应该长下面这个样子:
正确输出应为:
10 5 7 3
10 5 3 7
但若按照《算法笔记》的解法输出则为:
10 5 3 7
10 5 7 3
可以看出只能关注到直接儿子结点的顺序,而无法顾及孙子结点的顺序
正确的参考代码:
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional> // 包含greater/less的头文件
using namespace std;
struct node{
int weight;
vector<int> child;
}tree[105];
int n, m, s;
vector<vector<int> > ans; // 所有路径
vector<int> path; // 一条完整路径
void DFS(int index, int sum){
node cur = tree[index];
path.push_back(cur.weight);
sum += cur.weight;
if(sum > s) return;
if(sum == s){
if(tree[index].child.size() > 0)
return;
ans.push_back(path);
return;
}
for(int i = 0; i < cur.child.size(); i++){
DFS(cur.child[i], sum);
path.pop_back();
}
}
int main()
{
scanf("%d %d %d", &n, &m, &s);
for(int i = 0; i < n; i++){
scanf("%d", &tree[i].weight);
}
for(int i = 0; i < m; i++){
int index, num, tmp;
scanf("%d %d", &index, &num);
for(int j = 0; j < num; j++){
scanf("%d", &tmp);
tree[index].child.push_back(tmp);
}
}
DFS(0, 0); // DFS搜寻所有可能的完整路径
sort(ans.begin(), ans.end(), greater<vector<int> >());
for(int i = 0; i < ans.size(); i++){
int len = ans[i].size();
for(int j = 0; j < len; j++){
printf("%d", ans[i][j]);
if(j == len - 1) printf("\n");
else printf(" ");
}
}
return 0;
}
最后贴上《算法笔记》的参考代码(无法通过最后一个测试点),请酌情参考:
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 110;
struct node {
int weight;
vector<int> child;
} Node[maxn];
bool cmp(int a, int b) {
return Node[a].weight > Node[b].weight;
}
int n, m, s; // 结点数,边数,给定和
int path[maxn];
// 当前访问结点index,当前路径path上的结点个数numNode,当前结点加权和sum
void DFS(int index, int numNode, int sum) {
if (sum > s) return;
if (sum == s) {
if (Node[index].child.size() != 0) return; // 未到达叶子结点
for (int i = 0; i < numNode; i++) { // 到达叶子结点,输出一条完整路径
printf("%d", Node[path[i]].weight);
if (i < numNode - 1)
printf(" ");
else printf("\n");
}
return;
}
for (int i = 0; i < Node[index].child.size(); i++) { // 枚举所有子结点
int child = Node[index].child[i]; // 第i个子结点,加入路径
path[numNode] = child;
DFS(child, numNode + 1, sum + Node[child].weight); // 递归下一层
}
}
int main() {
scanf("%d %d %d", &n, &m, &s);
for (int i = 0; i < n; i++) {
scanf("%d", &Node[i].weight);
}
int id, k, child;
for (int i = 0; i < m; i++) {
scanf("%d %d", &id, &k); // 结点编号,子结点个数
for (int j = 0; j < k; j++) {
scanf("%d", &child);
Node[id].child.push_back(child);
}
// 注意是此处 排序 的问题!!
sort(Node[id].child.begin(), Node[id].child.end(), cmp);
}
path[0] = 0; // 路径第一个结点设为0号结点
DFS(0, 1, Node[0].weight);
return 0;
}