强调:在读入时对每个结点的所有子结点按权值从大到小排序,这样在递归的过程中总是会先访问所有子结点中权值更大的
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=110;
struct node
{
int weight;
vector<int> child;
}Node[maxn];
bool cmp(int a,int b)///***
{
return Node[a].weight>Node[b].weight;///对一个结点的每个子结点的权值排序
}
int n,m,s;
int path[maxn];
void DFS(int index,int numNode,int sum)
{
if(sum>s) return;
if(sum==s)
{
if(Node[index].child.size()!=0) return;
for(int i=0;i<numNode;i++)
{
printf("%d",Node[path[i]].weight);
if(i<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; ///***下标与个数正好差1
DFS(child,numNode+1,sum+Node[child].weight);
}
}
int main()
{
int weight;
int parent,k,child;
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",&parent,&k);
for(int j=0;j<k;j++)
{
scanf("%d",&child);
Node[parent].child.push_back(child); ///***
}
sort(Node[parent].child.begin(),Node[parent].child.end(),cmp);///***存放的是每个结点的子结点标号
}
DFS(0,1,Node[0].weight); ///****
return 0;
}