PATA1053 Path of Equal Weight

Given a non-empty tree with root R, and with weight W​i​​ assigned to each tree node T​i​​. 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 W​i(<1000) corresponds to the tree node T​i​​. Then M lines follow, each in the format:

1
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 {A​1​​,A​2​​,⋯,A​n​​} is said to be greater than sequence {B1
​​,B​2​​,⋯,B​m​​} if there exists 1≤k<min{n,m} such that A​i​​=B​i​​ for i=1,⋯,k, and A​k+1​​>B​k+1​​.

Sample Input:

1
2
3
4
5
6
7
8
9
10
11
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:

1
2
3
4
10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2

题目大意:
一棵结点数为N的树,每个结点对应不同权值,从根结点开始每次下层结点走,走到叶节点为止形成一条完整路径,若该路径上结点权值相加为S,则输出此路径。路径按非递减排序(路径大小规则:第一个结点权值一样就比第二个结点权值,大的在前面,以此类推)
思路及注意事项:
一个典型的树DFS遍历。把每个结点的孩子节点按权值从大到小排列,这样在遍历的过程中就能保证大的路径被优先遍历到;路径可以用数组存储也可以用变长数组存储;以sum=s为边界条件,此时判断是否到达叶子结点,如果到达,则输出当前路径,否则返回上层。

用path[]数组存储路径的方法,dfs需要记录当前存储路径结点的个数numN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct node{
int data;
vector<int> child;
};
vector<node> v;
bool cmp(int a,int b){
return v[a].data>v[b].data;
}
int n,m,s;
int path[100];
void dfs(int index,int numN,int sum){
if(sum==s){
if(v[index].child.size()==0){
for(int i=0;i<numN;i++){
printf("%d",v[path[i]].data);
if(i<numN-1) printf(" ");
else printf("\n");
}
return;
}else return;
}
for(int i=0;i<v[index].child.size();i++){
int child=v[index].child[i];
if(sum+v[child].data<=s){ //此处可以这样剪枝
path[numN]=child;
dfs(child,numN+1,sum+v[child].data);
}
}
}
int main(){
int x,k,ch;
scanf("%d%d%d",&n,&m,&s);
v.resize(n);
for(int i=0;i<n;i++)
scanf("%d",&v[i].data);
for(int i=0;i<m;i++){
scanf("%d%d",&x,&k);
for(int j=0;j<k;j++){
scanf("%d",&ch);
v[x].child.push_back(ch);
}
sort(v[x].child.begin(),v[x].child.end(),cmp);
}
path[0]=0;
dfs(0,1,v[0].data);
return 0;
}

也可以用vector< int > path存储路径,此法dfs中不需要开设numN记录当前结点数
但需务必记得在递归向上层返回时将此前push的结点pop出来~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
vector<int> path;
void dfs(int index,int sum){
if(sum==s){
if(v[index].child.size()==0){
for(int i=0;i<path.size();i++){
printf("%d",v[path[i]].data);
if(i<path.size()-1) printf(" ");
else printf("\n");
}
return;
}else return;
}
for(int i=0;i<v[index].child.size();i++){
int child=v[index].child[i];
if(sum+v[child].data<=s){
path.push_back(child);
dfs(child,sum+v[child].data);
path.pop_back(); //这步不能忘
}
}
}