题目描述
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 Figure 1: 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
Figure 1.
输入描述
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 < 230, 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.
输出描述
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.
输入样例
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
输出样例
10 5 2 7
10 4 10
10 3 3 6 2
10 3 3 6 2
题目大意
给一棵树,包含N个结点及权值,给定一个数S,求从根结点出发到达叶子结点的权值之和等于S的所有权值序列,将权值序列按第减的次序依次输出。
分析
静态建立该树,定义树的结点包含当前结点的编号,权重以及vector数组child,保存所有孩子节点的编号。在建立树时对每一个非叶子节点将其所有孩子节点按权值递增的顺序排序。然后对该树进行先序遍历,每向下遍历到一个结点,累加其权值,并将编号保存在数组w中,如果满足条件,输出w。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 101;
int N, M, S;
struct node { //结点定义
int num;//编号
int weight;//权重
vector<int> child;//保存所有孩子
} Node[MAX];
vector<int> w;//存放满足条件的一组结点的编号
bool cmp(int w1, int w2) {//对每个结点的所有孩子按权重进行排序
return Node[w1].weight > Node[w2].weight;
}
void preOrder(int num, int sum) {//先序遍历,num为当前结点的编号,sum为从权重和
sum += Node[num].weight;//累加上当前结点的权重
w.push_back(num);//将当前结点的编号压入数组w中
if (sum == S && Node[num].child.size() == 0) {//sum达到S且当前结点为叶子结点,此时w中便保存着符合条件的结点的编号
for (int i = 0; i < w.size(); ++i) {//按要求输出权值
if (i < w.size() - 1)
cout << Node[w[i]].weight << " ";
else cout << Node[w[i]].weight;
}
cout << endl;
return;
} else if (sum > S) return;
for (int i = 0; i < Node[num].child.size(); ++i) {//从根结点开始先序遍历整棵树
preOrder(Node[num].child[i], sum);
w.pop_back();//返回上一层,删除最后压入的元素
}
}
int main() {
cin >> N >> M >> S;
for (int i = 0; i < N; ++i) {
cin >> Node[i].weight;//按编号给结点赋权值
}
for (int j = 0; j < M; ++j) {//输入非叶子结点及其所有孩子结点
int childCount, parentNum;
cin >> parentNum >> childCount;
for (int k = 0; k < childCount; ++k) {
int childNum;
cin >> childNum;
Node[parentNum].child.push_back(childNum);
}
sort(Node[parentNum].child.begin(), Node[parentNum].child.end(), cmp);//对孩子结点按权值递减排序
}
preOrder(0, 0);
return 0;
}