-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1988D.cpp
105 lines (88 loc) · 2.15 KB
/
1988D.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
94
95
96
97
98
99
100
101
102
103
104
105
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int MAXN = 300005;
#ifdef DBG
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#else
#define dbg(...)
#endif
ll n, t;
ll a[MAXN];
vector<int> adj[MAXN];
const int k = 20;
ll f[MAXN][k];
void reset_globals()
{
fill(a, a + n, 0);
memset(f, 0, sizeof(ll) * n * k);
for (int i = 0; i < n; i++) {
adj[i].clear();
}
}
void dfs(int u, int par)
{
// f[u][3] = a[u] * (3+1) + \sum_v \min{f[v][any but != 3]
for (int i = 0; i < k; i++) {
f[u][i] = a[u] * (i + 1);
}
for (int v : adj[u]) {
if (v == par)
continue;
dfs(v, u);
static ll suffix_min[k], prefix_min[k];
suffix_min[k - 1] = f[v][k - 1];
for (int i = k - 2; i >= 0; i--) {
suffix_min[i] = min(suffix_min[i + 1], f[v][i]);
}
prefix_min[0] = f[v][0];
for (int i = 1; i < k; i++) {
prefix_min[i] = min(prefix_min[i - 1], f[v][i]);
}
for (int i = 0; i < k; i++) {
if (i == 0) {
f[u][i] += suffix_min[i + 1];
} else if (i + 1 == k) {
f[u][i] += prefix_min[i - 1];
} else {
f[u][i] += min(prefix_min[i - 1], suffix_min[i + 1]);
}
}
}
}
ll solve()
{
// Implement your solution here and return the result
dfs(0, -1);
ll ans = -1;
for (int i = 0; i < k; i++) {
if (ans == -1 || f[0][i] < ans) {
ans = f[0][i];
}
}
return ans;
}
int main()
{
scanf("%lld", &t);
while (t--) {
scanf("%lld", &n);
// dbg("n = %d\n", n);
for (int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
}
for (int i = 0; i < n - 1; i++) {
int x, y;
scanf("%d %d", &x, &y);
x--;
y--; // zero-indexing if the problem requires
adj[x].push_back(y);
adj[y].push_back(x);
}
// Call the solve function and print its result
printf("%lld\n", solve());
// Reset the global input data
reset_globals();
}
return 0;
}