/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Accepted 2ms 332.0 KiB
#3 Accepted 2ms 492.0 KiB
#4 Wrong Answer 2ms 332.0 KiB
#5 Accepted 2ms 328.0 KiB
#6 Wrong Answer 2ms 336.0 KiB

Code

#include <bits/stdc++.h>

using namespace std;

void solve(int cs) {
  int n, k;
  cin >> n >> k;
  vector<int64_t> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v; cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  /*
  vector<int64_t> sofar(n, 0);
  int LG = 0;
  while ((1 << LG) <= n) LG += 1;
  vector<int> depth(n, 0);
  vector<vector<int>> up(n, vector<int>(LG)), down(n, vector<int>(LG));
  auto dfs = [&](auto &&self, int node, int pr = -1) -> void {
    sofar[node] += a[node];
    for (auto &son : g[node]) {
      if (son == pr) continue;
      sofar[son] += sofar[node];
      depth[son] = depth[node] + 1;
      up[son][0] = node;
      down[node][0] = son;
      for (int i = 1; i < LG; i++) up[son][i] = up[up[son][i - 1]][i - 1];
      for (int i = 1; i < LG; i++) down[down][i] = down[down[down][i - 1]][i - 1];
      self(self, son, node);
    }
  };
  dfs(dfs, 0);
  auto getupper = [&](int u, int to) -> int {    
    for (int i = LG - 1; ~i; --i) {
      if (to >> i & 1) {
        u = up[u][i];
      }
    }
    return u;
  };
  auto getdown = [&](int u, int to) -> int {    
    for (int i = LG - 1; ~i; --i) {
      if (to >> i & 1) {
        u = up[u][i];
      }
    }
    return u;
  };
  int64_t ans = 0;
  for (int i = 1; i < n; i++) {
    if (depth[i] + 1 >= k) {
      int u = getupper(i, k - 1);
      if (u == 0) ans = max(ans, sofar[i]);
      else {
        u = up[u][0];
       ans = max(ans, sofar[i] - sofar[u]);
      }
    } else {

    }
  }
  cout << ans << "\n";
  */
  vector<int> par(n, 0);
  auto Dfs = [&](auto &&self, int node, int pr) -> void {
    par[node] = pr;
    for (auto &son : g[node]) {
      if (son ^ pr) {
        self(self, son, node);
      }
    }
  };
  Dfs(Dfs, 0, -1);
  int64_t ans = 0;
  auto dfs = [&](auto &&self, int node, int pr, int len, int64_t sum) -> void {
    if (len == k) {
      ans = max(ans, sum);
    } else {
      for (auto &son : g[node]) {
        if (son ^ pr) {
          self(self, son, node, len + 1, sum + a[son]);
        }
      }
    }
  };
  for (int i = 0; i < n; i++) {
    dfs(dfs, i, par[i], 1, a[i]);
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(!cin.tie(0));
  int t = 1;
  // cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve(i);
  }
  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:43:32
Judged At
2025-02-17 15:43:32
Judged By
Score
8
Total Time
2ms
Peak Memory
492.0 KiB