-
Notifications
You must be signed in to change notification settings - Fork 0
/
2213.cpp
93 lines (78 loc) · 1.67 KB
/
2213.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
91
92
93
#include <iostream>
#include <algorithm>
#include <vector>
#define N 10005
using namespace std;
int n, arr[N], d[N][2], check[N], val;
bool matrix[N][N];
vector<int> ans;
int eval(int index, int isSel)
{
int i;
if (d[index][isSel])
return d[index][isSel];
check[index] = 1;
if (isSel)
d[index][isSel] = arr[index];
for (i = 1; i <= n; i++)
{
if (matrix[index][i] && !check[i])
{
if (isSel)
d[index][isSel] += eval(i, 0);
else
d[index][isSel] += max(eval(i, 0), eval(i, 1));
}
}
check[index] = 0;
return d[index][isSel];
}
int find(int index, int isSel)
{
int i;
if (isSel)
ans.push_back(index);
check[index] = 1;
for (i = 1; i <= n; i++)
{
if (matrix[index][i] && !check[i])
{
if (isSel)
find(i, 0);
else
{
if (d[i][0] < d[i][1])
find(i, i);
else
find(i, 0);
}
}
}
check[index] = 0;
return d[index][isSel];
}
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int i, a, b;
cin >> n;
for (i = 1; i <= n; i++)
cin >> arr[i];
for (i = 1; i < n; i++)
{
cin >> a >> b;
matrix[a][b] = true;
matrix[b][a] = true;
}
val = max(eval(1, 0), eval(1, 1));
int res = 0;
if (d[1][0] < d[1][1])
res = 1;
cout << val << endl;
find(1, res);
sort(ans.begin(), ans.end());
for (i = 0; i < ans.size(); i++)
cout << ans[i] << " ";
return 0;
}