結果

問題 No.1221 木 *= 3
ユーザー MayimgMayimg
提出日時 2020-09-04 22:50:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 2,260 ms
コンパイル使用メモリ 212,212 KB
実行使用メモリ 25,472 KB
最終ジャッジ日時 2024-05-05 03:24:04
合計ジャッジ時間 4,451 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 97 ms
25,472 KB
testcase_08 AC 98 ms
24,960 KB
testcase_09 AC 92 ms
25,344 KB
testcase_10 AC 93 ms
24,960 KB
testcase_11 AC 97 ms
25,216 KB
testcase_12 AC 73 ms
16,440 KB
testcase_13 AC 68 ms
16,572 KB
testcase_14 AC 69 ms
16,440 KB
testcase_15 AC 71 ms
16,436 KB
testcase_16 AC 70 ms
16,440 KB
testcase_17 AC 87 ms
15,104 KB
testcase_18 AC 79 ms
15,104 KB
testcase_19 AC 82 ms
14,976 KB
testcase_20 AC 83 ms
15,104 KB
testcase_21 AC 80 ms
15,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0