/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.77 MiB
#2 Accepted 3ms 2.77 MiB
#3 Accepted 3ms 2.77 MiB
#4 Accepted 4ms 2.77 MiB
#5 Accepted 4ms 2.77 MiB
#6 Accepted 10ms 2.715 MiB
#7 Accepted 10ms 2.777 MiB
#8 Accepted 10ms 2.77 MiB
#9 Accepted 10ms 2.77 MiB
#10 Accepted 10ms 2.805 MiB
#11 Accepted 10ms 2.836 MiB
#12 Accepted 96ms 7.52 MiB
#13 Accepted 108ms 7.391 MiB
#14 Accepted 95ms 7.566 MiB
#15 Accepted 101ms 7.477 MiB
#16 Accepted 93ms 7.52 MiB
#17 Accepted 99ms 7.52 MiB
#18 Accepted 99ms 7.52 MiB
#19 Accepted 111ms 7.52 MiB
#20 Accepted 92ms 7.566 MiB
#21 Accepted 122ms 7.637 MiB
#22 Accepted 95ms 7.637 MiB
#23 Accepted 106ms 7.633 MiB
#24 Accepted 127ms 7.52 MiB
#25 Accepted 101ms 7.496 MiB
#26 Accepted 92ms 7.52 MiB
#27 Accepted 96ms 7.52 MiB
#28 Accepted 111ms 7.562 MiB
#29 Accepted 112ms 7.52 MiB
#30 Accepted 104ms 7.57 MiB
#31 Accepted 111ms 7.629 MiB
#32 Accepted 114ms 7.52 MiB
#33 Accepted 128ms 7.637 MiB
#34 Accepted 89ms 7.629 MiB
#35 Accepted 100ms 7.566 MiB
#36 Accepted 123ms 7.52 MiB
#37 Accepted 112ms 7.441 MiB
#38 Accepted 95ms 7.629 MiB
#39 Accepted 102ms 7.637 MiB
#40 Accepted 93ms 7.52 MiB
#41 Accepted 139ms 7.52 MiB
#42 Accepted 59ms 7.316 MiB
#43 Accepted 62ms 7.402 MiB
#44 Accepted 71ms 7.848 MiB
#45 Accepted 44ms 7.77 MiB
#46 Accepted 43ms 7.895 MiB
#47 Wrong Answer 107ms 15.02 MiB
#48 Wrong Answer 110ms 11.27 MiB

Code

#include <bits/stdc++.h>
using namespace std;

#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
using ll = long long;

const ll INF = 1'000'000'000'000'000'000;
const int MX = 100001;
const int MX_K = 301;
vector<int> g[MX];
ll a[MX];
ll curr_path[MX_K], mx_path[MX_K];
bool taken[MX];
int sub[MX], centroid[MX];
ll ANS;
int k;

void sub_dfs(int u, int p = -1) {
  sub[u] = 1;

  for (int v : g[u]) {
    if ((v == p) || taken[v]) continue;
    sub_dfs(v, u);
    sub[u] += sub[v];
  }
}

int find_centroid(int mx_sub, int u, int p = -1) {
  for (int v : g[u]) {
    if ((v == p) || taken[v]) continue;
    if (sub[v] > mx_sub) return find_centroid(mx_sub, v, u);
  }
  return u;
}

void go(int dep, ll val, int u, int p = -1) {
  curr_path[dep] = max(curr_path[dep], val);
  for (int v : g[u]) if (!taken[v]) {
    if (v == p) continue;
    go(dep + 1, val + a[v], v, u);
  }
}

void decompose(int u, int p = -1) {
  sub_dfs(u);

  int root = find_centroid(sub[u]/2, u);
  taken[root] = 1;
  centroid[root] = (p == -1) ? root : p;
  
  int i;
  for (i = 0; i <= k; ++i) mx_path[i] = -INF;
  mx_path[0] = a[root];
  
  for (int v : g[root]) if (!taken[v]) {
    for (i = 0; i <= k; ++i) curr_path[i] = -INF;
    curr_path[0] = a[root];
    go(1, a[root] + a[v], v, root);
    for (i = 0; i <= k; ++i) {
      ANS = max(ANS, curr_path[i] + mx_path[k-i] - a[root]);
    }
    for (i = 0; i <= k; ++i) {
      mx_path[i] = max(mx_path[i], curr_path[i]);
    }
  }

  ANS = max(ANS, mx_path[k]);

  for (int v : g[root]) {
    if (taken[v]) continue;
    decompose(v, root);
  }
}

void init(int n) {
  for (int i = 0; i <= n; ++i) {
    g[i].clear();
    taken[i] = 0;
  }
}

int main() {
  FAST;

  int n, i, u, v;
  cin >> n >> k;
  --k;
  init(n);

  for (i = 0; i < n; ++i) cin >> a[i];

  for (i = 1; i < n; ++i) {
    cin >> u >> v;
    --u; --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  ANS = 0;
  decompose(0);

  cout << ANS << "\n";

  return 0;
}

Information

Submit By
Type
Submission
Problem
P1161 Max path sum (Hard Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 15:34:06
Judged At
2025-02-17 15:34:06
Judged By
Score
92
Total Time
139ms
Peak Memory
15.02 MiB