# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
104463 | luciocf | Chase (CEOI17_chase) | C++14 | 706 ms | 252524 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int maxk = 110;
typedef long long ll;
int n, k;
int a[maxn];
ll soma[maxn];
ll d[2][maxn][maxk], ans;
ll ant[maxn][maxk];
vector<int> grafo[maxn];
void dfs(int u, int p)
{
for (auto v: grafo[u])
{
if (v == p) continue;
dfs(v, u);
for (int i = 1; i <= k; i++)
{
ll D = max(d[0][v][i], d[0][v][i-1]+soma[v]-1ll*a[u]);
if (D > d[0][u][i]) d[1][u][i] = d[0][u][i], d[0][u][i] = D;
else if (D > d[1][u][i]) d[1][u][i] = D;
}
}
}
void rotate(int u, int p)
{
for (int i = 1; i <= k; i++)
ans = max({ans, d[0][u][i], soma[u]+d[0][u][i-1]});
for (auto v: grafo[u])
{
if (v == p) continue;
for (int i = 1; i <= k; i++)
{
ant[u][i] = d[0][u][i];
ll D = max(d[0][v][i], d[0][v][i-1]+soma[v]-1ll*a[u]);
if (d[0][u][i] == D) d[0][u][i] = d[1][u][i];
}
for (int i = 1; i <= k; i++)
{
ll D = max(d[0][u][i], d[0][u][i-1]+soma[u]-1ll*a[v]);
if (D > d[0][v][i]) d[1][v][i] = d[0][v][i], d[0][v][i] = D;
else if (D > d[1][v][i]) d[1][v][i] = D;
}
rotate(v, u);
for (int i = 1; i <= k; i++)
d[0][u][i] = ant[u][i];
}
}
int main(void)
{
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d %d", &u, &v);
grafo[u].push_back(v); grafo[v].push_back(u);
}
for (int i = 1; i <= n; i++)
for (auto v: grafo[i])
soma[i] += 1ll*a[v];
dfs(1, 0);
rotate(1, 0);
printf("%lld\n", ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |