DFS:首先构建好DFS的格式,因为求和,所以带上sum
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn = 110;
struct node {
int weight;
vector<int>child;
}Node[maxn];
int n, m, s;
bool comp(int a, int b)
{
return Node[a].weight > Node[b].weight;
}
vector<int>ans;
void DFS(int root,int sum)
{
if (Node[root].child.size() == 0)
{
if (sum == s)
{
for (int i = 0; i < ans.size(); i++)
{
printf("%d", Node[ans[i]].weight);
if (i != ans.size() - 1)printf(" ");
else printf("\n");
}
}
return;
}
if (sum > s)return;
for (int i = 0; i < Node[root].child.size(); i++)
{
int child = Node[root].child[i];
ans.push_back(child);
DFS(child, sum + Node[child].weight);
ans.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++)
{
int id, k;
scanf("%d%d", &id, &k);
while (k--)
{
int x;
scanf("%d", &x);
Node[id].child.push_back(x);
}
sort(Node[id].child.begin(), Node[id].child.end(), comp);
}
ans.push_back(0);
DFS(0,Node[0].weight);
return 0;
}