結果
問題 | No.1221 木 *= 3 |
ユーザー |
|
提出日時 | 2020-09-04 22:50:52 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 112 ms / 2,000 ms |
コード長 | 982 bytes |
コンパイル時間 | 2,187 ms |
コンパイル使用メモリ | 204,952 KB |
最終ジャッジ日時 | 2025-01-14 06:16:13 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 18 |
ソースコード
#define _USE_MATH_DEFINES#include <bits/stdc++.h>using namespace std;int n;vector<int> a, b;vector<vector<int>> g;vector<vector<long long>> dp;void dfs (int cur, int par) {dp[cur][0] = a[cur];vector<long long> v;for (int child : g[cur]) if (child != par) {dfs (child, cur);dp[cur][0] += max(dp[child][0], dp[child][1]);v.push_back(dp[child][1]);}dp[cur][1] = 0;for (int child : g[cur]) if (child != par) {dp[cur][1] += max(dp[child][0], dp[child][1] + b[cur] + b[child]);}}signed main() {ios::sync_with_stdio(false); cin.tie(0);cin >> n;a = vector<int>(n);b = a;g = vector<vector<int>>(n);dp = vector<vector<long long>>(n, vector<long long>(2));for (auto& x : a) cin >> x;for (auto& x : b) cin >> x;for (int i = 0; i < n - 1; i++) {int u, v;cin >> u >> v;v--;u--;g[u].push_back(v);g[v].push_back(u);}dfs(0, -1);cout << max(dp[0][0], dp[0][1]) << endl;return 0;}