-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02213_Tree_DP.cpp
More file actions
80 lines (71 loc) · 1.63 KB
/
02213_Tree_DP.cpp
File metadata and controls
80 lines (71 loc) · 1.63 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX = 10000;
int n;
int costs[MAX + 1];
int DP[2][MAX + 1];
vector<int> graph[MAX + 1];
vector<int> link[2][MAX + 1];
void DFS(int node)
{
DP[1][node] += costs[node];
for (int next : graph[node])
{
if (DP[1][next] == 0)
{
DFS(next);
if (DP[0][next] > DP[1][next])
{
DP[0][node] += DP[0][next];
for (int temp : link[0][next])
link[0][node].push_back(temp);
}
else
{
DP[0][node] += DP[1][next];
for (int temp : link[1][next])
link[0][node].push_back(temp);
}
DP[1][node] += DP[0][next];
for (int temp : link[0][next])
link[1][node].push_back(temp);
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> costs[i];
link[1][i].push_back(i);
}
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
DFS(1);
if (DP[0][1] > DP[1][1])
{
cout << DP[0][1] << '\n';
sort(link[0][1].begin(), link[0][1].end());
for (int temp : link[0][1])
cout << temp << ' ';
}
else
{
cout << DP[1][1] << '\n';
sort(link[1][1].begin(), link[1][1].end());
for (int temp : link[1][1])
cout << temp << ' ';
}
return 0;
}