結果
問題 |
No.1221 木 *= 3
|
ユーザー |
![]() |
提出日時 | 2020-09-05 05:42:35 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 137 ms / 2,000 ms |
コード長 | 1,143 bytes |
コンパイル時間 | 3,800 ms |
コンパイル使用メモリ | 253,108 KB |
最終ジャッジ日時 | 2025-01-14 06:57:31 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#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'; }