/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 3ms 2.777 MiB
#2 Accepted 4ms 4.027 MiB
#3 Accepted 3ms 2.777 MiB
#4 Accepted 3ms 4.277 MiB
#5 Accepted 3ms 2.781 MiB
#6 Accepted 3ms 3.77 MiB
#7 Accepted 3ms 3.75 MiB
#8 Accepted 3ms 3.32 MiB
#9 Accepted 3ms 4.133 MiB
#10 Accepted 3ms 3.277 MiB
#11 Accepted 4ms 4.027 MiB
#12 Accepted 77ms 7.645 MiB
#13 Accepted 91ms 7.422 MiB
#14 Accepted 90ms 7.574 MiB
#15 Accepted 91ms 7.469 MiB
#16 Accepted 85ms 7.496 MiB
#17 Accepted 79ms 7.52 MiB
#18 Accepted 76ms 7.52 MiB
#19 Accepted 84ms 7.523 MiB
#20 Accepted 76ms 7.348 MiB
#21 Accepted 75ms 7.523 MiB
#22 Accepted 78ms 7.363 MiB
#23 Accepted 81ms 7.469 MiB
#24 Accepted 82ms 7.52 MiB
#25 Accepted 82ms 7.332 MiB
#26 Accepted 69ms 7.52 MiB
#27 Accepted 94ms 7.531 MiB
#28 Accepted 68ms 7.316 MiB
#29 Accepted 74ms 7.312 MiB
#30 Accepted 83ms 7.426 MiB
#31 Accepted 93ms 7.52 MiB
#32 Wrong Answer 163ms 15.176 MiB
#33 Wrong Answer 143ms 15.172 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
P1160 Max path sum (Easy Version)
Contest
Brain Booster #8
Language
C++17 (G++ 13.2.0)
Submit At
2025-02-17 15:35:10
Judged At
2025-02-17 15:35:10
Judged By
Score
62
Total Time
163ms
Peak Memory
15.176 MiB