-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1053-1.cpp
67 lines (61 loc) · 1.19 KB
/
A1053-1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* author: zhouyuhao
* created: 2023-03-29 13:18:41
* modified: 2023-03-29 16:43:01
* item: Programming Ability Test
* site: Yuting
*/
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int s;
vector<int> w, temp;
vector<vector<int>> t, ans;
void dfs(int r, int sum, int l) {
temp.resize(l + 1);
temp[l] = w[r];
sum += w[r];
if (t[r].empty()) {
if (sum == s) {
ans.emplace_back(temp);
}
return;
}
if (sum > s) {
return;
}
for (int i = 0; i < (int)t[r].size(); i++) {
dfs(t[r][i], sum, l + 1);
}
}
int main(int argc, char const *argv[]) {
int n, m;
cin >> n >> m >> s;
w.resize(n);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
t.resize(n);
vector<bool> isroot(n, true);
for (int i = 0; i < m; i++) {
int id, k;
cin >> id >> k;
for (int j = 0; j < k; j++) {
int c;
cin >> c;
t[id].emplace_back(c);
isroot[c] = false;
}
}
int r = find(isroot.begin(), isroot.end(), true) - isroot.begin();
dfs(r, 0, 0);
sort(ans.begin(), ans.end(), greater<vector<int>>());
for (auto it : ans) {
for (int i = 0; i < (int)it.size(); i++) {
cout << it[i];
i < (int)it.size() - 1 ? cout << " " : cout << "\n";
}
}
return 0;
}