-
Notifications
You must be signed in to change notification settings - Fork 0
/
1053.cpp
90 lines (73 loc) · 1.84 KB
/
1053.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <cstdio>
#include <vector>
#include <algorithm>
const int MAXN = 100 + 10;
std::vector<int> ans[MAXN];
typedef struct Node {
struct Node *ch, *next;
struct Node *fa;
int weight, sum;
} Node;
Node nodes[MAXN];
void addChild(Node *fa, Node *ch) {
ch->fa = fa;
ch->next = fa->ch;
fa->ch = ch;
}
void dfs(Node *x) {
if (x->fa) x->sum += x->fa->sum;
for (Node *c = x->ch; c; c = c->next) {
dfs(c);
}
}
void solve(Node *x, int cnt) {
if (x->fa) solve(x->fa, cnt);
ans[cnt].push_back(x->weight);
}
bool cmp(std::vector<int> a, std::vector<int> b) {
for (int i = 0; i < a.size() && i < b.size(); i++) {
if (a[i] == b[i]) continue;
else return a[i] > b[i];
}
return a.size() > b.size();
}
int main() {
int n, m, s;
scanf("%d%d%d", &n, &m, &s);
for (int i = 0; i < n; i++) {
scanf("%d", &nodes[i].weight);
nodes[i].sum = nodes[i].weight;
}
for (int i = 0; i < m; i++) {
int fa;
scanf("%d", &fa);
int num;
scanf("%d", &num);
for (int i = 0; i < num; i++) {
int ch;
scanf("%d", &ch);
addChild(nodes + fa, nodes + ch);
}
}
for (int i = 0; i < n; i++) {
if (!nodes[i].fa) {
dfs(nodes + i);
break;
}
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (!nodes[i].ch && nodes[i].sum == s) {
solve(nodes + i, cnt);
cnt++;
}
}
std::sort(ans, ans + cnt, cmp);
for (int i = 0; i < cnt; i++) {
for (int j = 0; j < ans[i].size(); j++) {
if (j == ans[i].size() - 1) printf("%d\n", ans[i][j]);
else printf("%d ", ans[i][j]);
}
}
return 0;
}