#include<stdio.h> #include<vector> #include<queue> #include<algorithm> using namespace std; const int MAX=1010; int n,m; //n个节点,m个非叶子节点 long long int S; //待测权值 long long int weight[MAX]; //每个节点的权值 vector<int> child[MAX]; //存储节点 bool cmp(int a,int b) { return weight[a]>weight[b]; } long long int sum=0; vector<int> path; //存路径权值 void DFS(int root) { if(child[root].size()==0) { if(sum==S) { printf("%d",path.front()); //权值S不为0,符合条件一定有路径。单独输出首个,控制后面空格数 for(int i=1 ; i<path.size() ; ++i) printf(" %d",path[i]); //输出剩余路径 printf("\n"); } return; } for(int i=0 ; i<child[root].size() ; ++i) { sum+=weight[child[root][i]]; //加上该孩子的权值 //printf("root=%d, i=%d, sum=%lld, child[root][i]=%d\n", root, i, sum, child[root][i]); path.push_back(weight[child[root][i]]); //该孩子加入到路径 DFS(child[root][i]); sum-=path[path.size()-1]; path.pop_back(); } } int main() { scanf("%d%d%lld",&n,&m,&S); for(int i=0 ; i<n ; ++i) scanf("%lld",&weight[i]); for(int i=0 ; i<m ; ++i) { int father,k,c; scanf("%d%d",&father,&k); for(int j=0 ; j<k ; ++j) { scanf("%d",&c); child[father].push_back(c); } sort(child[father].begin(),child[father].end(),cmp); //【skill】对vector进行排序,保证遍历的时候从大到小 } path.push_back(weight[0]); sum+=weight[0]; DFS(0); return 0; }
时间: 2024-08-26 18:43:02