输入时要做预处理,因为题目要求序列是从大往小输出。
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
struct Node{
int weight;
vector<int> child;
}node[110];
int n, m, s, id, k, ch;
vector<int> temp;
bool cmp(int a, int b)
{
return node[a].weight > node[b].weight;
}
void DFS(int root, int sumweight)
{
if(node[root].child.size() == 0){
if(sumweight == s){
temp.push_back(root);
for(int i = 0; i < temp.size(); i++){
printf("%d", node[temp[i]].weight);
if(i < temp.size() - 1) printf(" ");
else printf("\n");
}
temp.pop_back();
}
return;
}
temp.push_back(root);
for(int i = 0; i < node[root].child.size(); i++){
int x = node[root].child[i];
if(sumweight + node[x].weight <= s) DFS(x, sumweight + node[x].weight);
}
temp.pop_back();
}
int main()
{
scanf("%d%d%d", &n, &m, &s);
for(int i = 0; i < n; i++){
scanf("%d", &node[i].weight);
}
for(int i = 0; i < m; i++){
scanf("%d%d", &id, &k);
for(int j = 0; j < k; j++){
scanf("%d", &ch);
node[id].child.push_back(ch);
}
sort(node[id].child.begin(), node[id].child.end(), cmp);
}
DFS(0, node[0].weight);
return 0;
}