按照孩子的权值来进行排序,学到了
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 110;
struct BTnode {
int weight;
vector<int> child;
}node[maxn];
int path[maxn];
int n, m, s;
bool cmp(int a, int b) {
return node[a].weight > node[b].weight;
}
void DFS(int index, int numNode, int sum) {
if (sum > s) return;
if (sum == s) {
if (node[index].child.size() != 0) return;
for (int j = 0; j < numNode; j++) {
printf("%d", node[path[j]].weight);
if (j < numNode - 1) printf(" ");
else printf("\n");
}
return;
}
for (int i = 0; i < node[index].child.size(); i++) {
int child = node[index].child[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 parent, k, tmp;
for (int i = 0; i < m; i++) {
scanf("%d%d", &parent, &k);
for (int j = 0; j < k; j++) {
scanf("%d", &tmp);
node[parent].child.push_back(tmp);
}
sort(node[parent].child.begin(), node[parent].child.end(), cmp);
}
path[0] = 0;
DFS(0, 1, node[0].weight);
return 0;
}