It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ‘‘capturing‘‘ virtual girls in gal games. He is able to play \(k\) games simultaneously.
One day he gets a new gal game named ‘‘XX island‘‘. There are \(n\) scenes in that game, and one scene will be transformed to different scenes by choosing different options while playing the game. All the scenes form a structure like a rooted tree such that the root is exactly the opening scene while leaves are all the ending scenes. Each scene has a value , and we use \(w_i\) as the value of the i-th scene. Once Katsuragi entering some new scene, he will get the value of that scene. However, even if Katsuragi enters some scenes for more than once, he will get \(w_i\) for only once.
For his outstanding ability in playing gal games, Katsuragi is able to play the game \(k\) times simultaneously. Now you are asked to calculate the maximum total value he will get by playing that game for \(k\) times.
Input
The first line contains an integer \(T(T≤20)\), denoting the number of test cases.
For each test case, the first line contains two numbers \(n,k(1≤k≤n≤100000)\), denoting the total number of scenes and the maximum times for Katsuragi to play the game ‘‘XX island‘‘.
The second line contains \(n\) non-negative numbers, separated by space. The i-th number denotes the value of the i-th scene. It is guaranteed that all the values are less than or equal to 231 ? 1.
In the following \(n?1\) lines, each line contains two integers \(a,b(1≤a,b≤n)\), implying we can transform from the a-th scene to the b-th scene.
We assume the first scene(i.e., the scene with index one) to be the opening scene(i.e., the root of the tree).
Output
For each test case, output ‘‘Case #t:‘‘ to represent the t-th case, and then output the maximum total value Katsuragi will get.
思路:贪心的思想,最重的子链继承根到父亲的链,然后排序得到答案。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node {
ll to, nxt;
}e[200005]; ll head[100005], tot;
inline void add_e(ll u, ll v) {e[++tot].to = v; e[tot].nxt = head[u]; head[u] = tot;}
ll t[100005], val[100005], sum[100005], maxs[100005];
vector <ll> V;
void dfs1(ll u)
{
t[u] = val[u];
if (!head[u]) return;
ll tmp = 0;
for (ll i = head[u]; i; i = e[i].nxt)
{
ll v = e[i].to;
dfs1(v);
if (t[v] > tmp) tmp = t[v], maxs[u] = v;
} t[u] += tmp;
}
void dfs2(ll u)
{
sum[u] += val[u];
if (!head[u])
{
V.push_back(sum[u]);
}
for (ll i = head[u]; i; i = e[i].nxt)
{
ll v = e[i].to;
if (maxs[u] == v) sum[v] += sum[u];
dfs2(v);
}
}
inline void solve(ll T)
{
memset(head, 0, sizeof head), tot = 1;
memset(sum, 0, sizeof sum);
memset(t, 0, sizeof t);
memset(maxs, 0, sizeof maxs);
memset(val, 0, sizeof val);
V.clear();
ll n, k;
scanf("%lld%lld", &n, &k);
for (ll i = 1; i <= n; i++) scanf("%lld", &val[i]);
for (ll i = 1, x, y; i < n; i++) scanf("%lld%lld", &x, &y), add_e(x, y);
dfs1(1), dfs2(1);
sort(V.begin(), V.end(), greater <ll>() );
ll ans = 0, maxx = V.size();
for (ll i = 0; i < k && i < maxx; i++) ans += V[i];
printf("Case #%lld: %lld\n", T, ans);
}
int main()
{
ll T;
scanf("%lld", &T);
for (ll i = 1; i <= T; i++) solve(i);
}