#include <iostream>
#include <cstdio>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main(){
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
vector<int> weights(n);
for(int i = 0; i < n; ++i){
scanf("%d", &weights[i]);
}
vector<vector<int>> tree(n);
for(int i = 0; i < m; ++i){
int id, k;
scanf("%d%d", &id, &k);
tree[id].resize(k);
for(int j = 0; j < k; ++j){
scanf("%d", &tree[id][j]);
}
}
vector<int> path(1, weights[0]);
vector<vector<int>> paths;
function<void(int,int)> dfs = [&](int id, int sum){
if(tree[id].empty()){
if(sum == s) paths.push_back(path);
return;
}
if(sum > s) return;
for(auto& child : tree[id]){
path.push_back(weights[child]);
dfs(child, sum + weights[child]);
path.pop_back();
}
};
dfs(0, weights[0]);
sort(begin(paths), end(paths), [](const vector<int>& a, const vector<int>& b){
for(size_t i = 0; i < a.size() && i < b.size(); ++i){
if(a[i] < b[i]) return false;
else if(a[i] > b[i]) return true;
}
return false;
});
for(auto& path : paths){
for(size_t i = 0; i < path.size(); ++i){
if(i) printf(" ");
printf("%d", path[i]);
}
printf("\n");
}
return 0;
}
1053. Path of Equal Weight (30)
最新推荐文章于 2021-09-16 16:36:38 发布