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
谷歌中文翻译:
给定一个具有根R且权重Wi分配给每个树节点T i的非空树。 从R到L的路径的权重定义为沿着从R到任何叶节点L的路径的所有节点的权重之和。
现在给定任何加权树,您应该找到权重等于给定数字的所有路径。 例如,让我们考虑下图中显示的树:对于每个节点,较高的数字是节点ID,它是两位数字,而较低的数字是该节点的权重。 假设给定数为24,则存在4个具有相同给定权重的不同路径:{10 5 2 7},{10 4 10},{10 3 3 6 2}和{10 3 3 6 2}, 对应于图中的红色边缘。
输入规格:
每个输入文件包含一个测试用例。每种情况均以包含0 <N≤100的行,树中节点数,M(<N),非叶节点数和0 <S <2的30次方(数。下一行包含N个正数,其中W(<1000)对应于树节点Ti。然后是M行,每行的格式为:
ID K ID [1] ID [2] ... ID [K]
其中ID是代表给定非叶节点的两位数字,K是其子代的数目,后跟其子代的两位ID序列。为了简单起见,让我们将根ID固定为00。
输出规格:
对于每个测试用例,以不递增的顺序打印所有权重为S的路径。每个路径占据一条线,从根到叶按顺序打印权重。所有数字必须用空格分隔,行尾不得有多余的空格。
注意:如果存在序列{A1,A2,⋯,A,n}大于序列{B1,B2,⋯,Bm}存在1≤k<min {n,m},使得对于i = 1,⋯,k和Ak + 1> Bk + 1,Ai = Bi。
柳神大大无敌!
#include<bits/stdc++.h>
using namespace std;
struct node {
int w;
vector<int> child;
};
vector<node> v; //存放各个树结点
vector<int> path;
int n, m,t; //n个点,m个树结构,k ,t目标路径和
int nod, k; //nod结点上有k个子节点
bool cmp(int a, int b) {
return v[a].w > v[b].w;
}
void init() {
cin >> n >> m >> t;
v.resize(n);
path.resize(n);
for(int i = 0; i < n; i++) {
cin >> v[i].w;
}
for(int i = 0; i < m; i++) {
cin >> nod >> k;
v[nod].child.resize(k);
for(int j = 0; j < k; j++) {
cin >> v[nod].child[j];
}
//将结点权值从大到小排序
sort(v[nod].child.begin(), v[nod].child.end(), cmp);
}
}
void dfs(int index, int nodeNum, int sum) {
if(sum > t)
return ;
if(sum == t) {
if(v[index].child.size() != 0)
return ;
for(int i = 0; i < nodeNum; i++) {
cout << v[path[i]].w;
if(i != nodeNum - 1) {
cout << " ";
} else {
cout << endl;
}
}
return ;
}
for(int i = 0; i < v[index].child.size(); i++) {
//从孩子里继续深搜
int node = v[index].child[i];
//搜到的结点放入
path[nodeNum] = node;
//该节点,结点数,总权值
dfs(node, nodeNum + 1, sum + v[node].w);
}
}
int main() {
init();
dfs(0, 1, v[0].w);
return 0;
}