QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#535674#4815. Flower's LandJWRuixiRE 1ms9752kbC++202.5kb2024-08-28 12:52:432024-08-28 12:52:44

Judging History

你现在查看的是最新测评结果

  • [2024-08-28 12:52:44]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:9752kb
  • [2024-08-28 12:52:43]
  • 提交

answer

#ifdef LOCAL
#include "stdafx.h"
#else
#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define sz(v) ((int) (v).size())
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
using namespace std;

using vi = vector<int>;
#endif

constexpr int N = 4e4 + 9;
constexpr int M = 3e3 + 9;
int n, k, a[N];

vi G[N];

bool vis[N];

int sz[N], id1[N], id2[N], sq1[N], sq2[N], c1, c2, fa[N];

void dfs (int u, int f) {
  sz[u] = 1;
  fa[u] = f;
  sq1[id1[u] = ++c1] = u;
  for (int v : G[u]) {
    if (vis[v] || v == f) {
      continue;
    }
    dfs(v, u);
    sz[u] += sz[v];
  }
  sq2[id2[u] = ++c2] = u;
}

int calc_mx (int x, int all) {
  int mxs = all - sz[x];
  for (int y : G[x]) {
    if (vis[y] || sz[y] > sz[x]) {
      continue;
    }
    mxs = max(mxs, sz[y]);
  }
  return mxs;
}

int get_rt (int L, int R) {
  int all = sz[sq1[L]];
  return *min_element(sq1 + L, sq1 + R, [&] (int x, int y) {
    return calc_mx(x, all) < calc_mx(y, all);
  });
}

int f[N][M], g[N][M], ns[N];

void dfz (int u) {
  dfs(u, 0);
  vis[u] = true;
  int S = sz[u];
  f[S + 1][0] = g[0][0] = 0;
  L (i, 1, k) {
    f[S + 1][i] = g[0][i] = INT_MIN / 2;
  }
  R (i, S, 1) {
    int x = sq1[i];
    L (j, 0, k) {
      f[i][j] = f[i + sz[x]][j];
    }
    L (j, 1, k) {
      f[i][j] = max(f[i][j], f[i + 1][j - 1] + a[x]);
    }
  }
  L (i, 1, S) {
    int x = sq2[i];
    L (j, 0, k) {
      g[i][j] = g[i - sz[x]][j];
    }
    L (j, 1, k) {
      g[i][j] = max(g[i][j], g[i - 1][j - 1] + a[x]);
    }
  }
  L (i, 1, S) {
    int x = sq1[i];
    int w = 0, h = 0;
    for (int y = x; y; y = fa[y]) {
      w += a[y];
      ++h;
    }
    L (j, 0, k - h) {
      ns[x] = max(ns[x], f[i + 1][j] + g[id2[x] - sz[x]][k - h - j] + w);
    }
  }
  vi nx;
  for (int v : G[u]) {
    if (vis[v] || sz[v] < k) {
      continue;
    }
    nx.eb(get_rt(id1[u], id1[u] + sz[u]));
  }
  c1 = c2 = 0;
  for (int rt : nx) {
    dfz(rt);
  }
}

int main () {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k;
  L (i, 1, n) {
    cin >> a[i];
  }
  L (i, 1, n - 1) {
    int u, v;
    cin >> u >> v;
    G[u].eb(v);
    G[v].eb(u);
  }
  dfs(1, 0), c1 = c2 = 0;
  dfz(get_rt(1, n + 1));
  L (i, 1, n) {
    cout << ns[i] << " \n"[i == n];
  }
}
// I love WHQ!

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7700kb

input:

5 3
6 10 4 3 4
3 4
4 2
2 5
5 1

output:

20 20 17 17 20

result:

ok 5 number(s): "20 20 17 17 20"

Test #2:

score: 0
Accepted
time: 0ms
memory: 9752kb

input:

7 4
1 3 2 1 7 12 17
4 6
1 4
5 1
2 5
7 6
3 2

output:

31 13 13 31 21 31 31

result:

ok 7 numbers

Test #3:

score: 0
Accepted
time: 1ms
memory: 7980kb

input:

1 1
20

output:

20

result:

ok 1 number(s): "20"

Test #4:

score: -100
Runtime Error

input:

10 3
19 7 25 18 93 97 21 51 60 80
6 7
7 1
1 9
9 10
10 2
2 5
5 3
3 8
8 4

output:


result: