/ 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 3ms 2.773 MiB
#5 Accepted 4ms 2.812 MiB
#6 Accepted 4ms 2.773 MiB
#7 Accepted 7ms 2.77 MiB
#8 Accepted 8ms 2.77 MiB
#9 Accepted 7ms 2.816 MiB
#10 Accepted 10ms 2.824 MiB
#11 Accepted 10ms 2.77 MiB
#12 Accepted 97ms 7.598 MiB
#13 Accepted 103ms 7.633 MiB
#14 Accepted 120ms 7.621 MiB
#15 Accepted 102ms 7.504 MiB
#16 Accepted 93ms 7.52 MiB
#17 Accepted 122ms 7.566 MiB
#18 Accepted 102ms 7.562 MiB
#19 Accepted 124ms 7.633 MiB
#20 Accepted 96ms 7.465 MiB
#21 Accepted 99ms 7.52 MiB
#22 Accepted 93ms 7.387 MiB
#23 Accepted 100ms 7.637 MiB
#24 Accepted 96ms 7.523 MiB
#25 Accepted 123ms 7.602 MiB
#26 Accepted 119ms 7.598 MiB
#27 Accepted 97ms 7.512 MiB
#28 Accepted 117ms 7.52 MiB
#29 Accepted 110ms 7.508 MiB
#30 Accepted 97ms 7.637 MiB
#31 Accepted 108ms 7.523 MiB
#32 Accepted 111ms 7.52 MiB
#33 Accepted 108ms 7.52 MiB
#34 Accepted 89ms 7.387 MiB
#35 Accepted 98ms 7.57 MiB
#36 Accepted 91ms 7.547 MiB
#37 Accepted 108ms 7.52 MiB
#38 Accepted 93ms 7.398 MiB
#39 Accepted 124ms 7.633 MiB
#40 Accepted 95ms 7.52 MiB
#41 Accepted 110ms 7.566 MiB
#42 Accepted 78ms 7.312 MiB
#43 Accepted 88ms 7.395 MiB
#44 Accepted 43ms 7.723 MiB
#45 Accepted 72ms 7.855 MiB
#46 Accepted 66ms 7.77 MiB
#47 Wrong Answer 106ms 15.105 MiB
#48 Wrong Answer 104ms 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];
  
  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:27:56
Judged At
2025-02-17 15:27:56
Judged By
Score
92
Total Time
124ms
Peak Memory
15.105 MiB