結果

問題 No.1221 木 *= 3
ユーザー risujirohrisujiroh
提出日時 2020-09-05 05:42:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,143 bytes
コンパイル時間 4,102 ms
コンパイル使用メモリ 267,524 KB
実行使用メモリ 21,504 KB
最終ジャッジ日時 2024-05-05 12:10:31
合計ジャッジ時間 6,676 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 79 ms
21,504 KB
testcase_08 AC 77 ms
20,980 KB
testcase_09 AC 83 ms
21,504 KB
testcase_10 AC 83 ms
21,120 KB
testcase_11 AC 85 ms
21,248 KB
testcase_12 AC 66 ms
11,424 KB
testcase_13 AC 64 ms
11,420 KB
testcase_14 AC 68 ms
11,420 KB
testcase_15 AC 71 ms
11,552 KB
testcase_16 AC 69 ms
11,428 KB
testcase_17 AC 76 ms
11,136 KB
testcase_18 AC 74 ms
11,008 KB
testcase_19 AC 79 ms
11,136 KB
testcase_20 AC 77 ms
11,136 KB
testcase_21 AC 80 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>

template <class F>
struct y_combinator : private F {
  y_combinator(F f) : F(f) {}
  template <class... Args>
  decltype(auto) operator()(Args&&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

int main() {
  using namespace std;
  cin.tie(nullptr)->sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto&& e : a) cin >> e;
  for (auto&& e : b) cin >> e;
  vector<vector<int>> g(n);
  for (int _ = n - 1; _--;) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  // vertex v -> -a[v]
  // edge uv -> b[u] + b[v]

  vector<array<int64_t, 2>> dp(n);
  y_combinator([&](auto&& self, int v, int p) -> void {
    dp[v] = {a[v], 0};
    for (int u : g[v]) {
      if (u == p) continue;
      self(u, v);
      array<int64_t, 2> new_dp;
      new_dp.fill(-1e18);
      for (int x : {0, 1})
        for (int y : {0, 1})
          new_dp[x] =
              max(new_dp[x], dp[v][x] + dp[u][y] + x * y * (b[v] + b[u]));
      swap(dp[v], new_dp);
    }
  })(0, -1);

  cout << max(dp[0][0], dp[0][1]) << '\n';
}
0