結果

問題 No.1221 木 *= 3
ユーザー MayimgMayimg
提出日時 2020-09-04 22:50:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 982 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 211,660 KB
実行使用メモリ 23,980 KB
最終ジャッジ日時 2023-08-17 20:29:45
合計ジャッジ時間 5,846 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 114 ms
23,980 KB
testcase_08 AC 114 ms
23,700 KB
testcase_09 AC 115 ms
23,972 KB
testcase_10 AC 119 ms
23,620 KB
testcase_11 AC 117 ms
23,588 KB
testcase_12 AC 85 ms
16,296 KB
testcase_13 AC 82 ms
16,168 KB
testcase_14 AC 84 ms
16,216 KB
testcase_15 AC 83 ms
16,180 KB
testcase_16 AC 82 ms
16,176 KB
testcase_17 AC 111 ms
14,904 KB
testcase_18 AC 109 ms
14,864 KB
testcase_19 AC 109 ms
14,952 KB
testcase_20 AC 112 ms
14,824 KB
testcase_21 AC 105 ms
14,940 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