-
Notifications
You must be signed in to change notification settings - Fork 0
/
eulertour.cpp
66 lines (58 loc) · 1.3 KB
/
eulertour.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
#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;
const lli inf = 1e18;
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> w(n), end(n), path;
vector<bool> isend(2*n);
vector<lli> sum(n);
for(int & wi : w){
cin >> wi;
}
vector<vector<int>> adj(n);
for(int i = 0; i < n-1; ++i){
int u, v;
cin >> u >> v;
adj[u-1].push_back(v-1);
adj[v-1].push_back(u-1);
}
int t = 0;
function<void(int, int)> dfs = [&](int u, int p){
sum[u] = w[u];
t++;
path.push_back(u);
for(int v : adj[u]){
if(v == p) continue;
dfs(v, u);
sum[u] += sum[v];
}
end[u] = t++;
isend[end[u]] = true;
path.push_back(u);
};
dfs(0, -1);
vector<vector<lli>> mem(t+1, vector<lli>(k+1, -inf));
vector<vector<bool>> calc(t+1, vector<bool>(k+1));
function<lli(int, int)> f = [&](int pos, int rem){
if(calc[pos][rem]) return mem[pos][rem];
calc[pos][rem] = true;
lli & ans = mem[pos][rem];
if(pos == t){
return ans = (rem == 0 ? 0 : -inf);
}else if(isend[pos]){
return ans = f(pos + 1, rem);
}else if(rem == 0){
return ans = 0;
}
return ans = max(sum[path[pos]] + f(end[path[pos]] + 1, rem-1), f(pos + 1, rem));
};
if(f(0, k) <= -inf/100){
cout << "Impossible\n";
}else{
cout << f(0, k) << "\n";
}
return 0;
}