/ 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.812 MiB
#4 Accepted 3ms 2.77 MiB
#5 Accepted 3ms 2.77 MiB
#6 Accepted 3ms 2.77 MiB
#7 Accepted 3ms 2.77 MiB
#8 Accepted 3ms 2.77 MiB
#9 Accepted 3ms 2.77 MiB
#10 Accepted 6ms 2.68 MiB
#11 Accepted 7ms 2.77 MiB
#12 Accepted 123ms 7.52 MiB
#13 Accepted 132ms 7.633 MiB
#14 Accepted 96ms 7.605 MiB
#15 Accepted 101ms 7.52 MiB
#16 Accepted 119ms 7.562 MiB
#17 Accepted 96ms 7.484 MiB
#18 Accepted 98ms 7.523 MiB
#19 Accepted 103ms 7.52 MiB
#20 Accepted 95ms 7.633 MiB
#21 Accepted 106ms 7.52 MiB
#22 Accepted 95ms 7.562 MiB
#23 Accepted 110ms 7.555 MiB
#24 Accepted 98ms 7.566 MiB
#25 Accepted 95ms 7.641 MiB
#26 Accepted 114ms 7.535 MiB
#27 Accepted 97ms 7.562 MiB
#28 Accepted 89ms 7.562 MiB
#29 Accepted 128ms 7.633 MiB
#30 Accepted 122ms 7.465 MiB
#31 Accepted 108ms 7.508 MiB
#32 Accepted 113ms 7.426 MiB
#33 Accepted 109ms 7.52 MiB
#34 Accepted 118ms 7.613 MiB
#35 Accepted 99ms 7.52 MiB
#36 Accepted 97ms 7.52 MiB
#37 Accepted 112ms 7.637 MiB
#38 Accepted 108ms 7.613 MiB
#39 Accepted 116ms 7.434 MiB
#40 Accepted 110ms 7.559 MiB
#41 Accepted 131ms 7.629 MiB
#42 Accepted 61ms 7.52 MiB
#43 Accepted 62ms 7.52 MiB
#44 Accepted 48ms 7.77 MiB
#45 Accepted 47ms 7.77 MiB
#46 Accepted 45ms 7.918 MiB
#47 Wrong Answer 104ms 15.105 MiB
#48 Wrong Answer 108ms 11.344 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;
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];
  
  bool f = 0;
  for (int v : g[root]) if (!taken[v]) {
    for (i = 0; i <= k; ++i) curr_path[i] = -INF;
    go(1, a[root] + a[v], v, root);
    if (f) for (i = 1; i < k; ++i) {
      ANS = max(ANS, curr_path[i] + mx_path[k-i] - a[root]);
    }
    for (i = 1; i <= k; ++i) {
      mx_path[i] = max(mx_path[i], curr_path[i]);
    }
    f = 1;
  }

  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:25:57
Judged At
2025-02-17 15:25:57
Judged By
Score
92
Total Time
132ms
Peak Memory
15.105 MiB