結果

問題 No.1221 木 *= 3
ユーザー chocono2230chocono2230
提出日時 2020-09-05 19:04:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,355 bytes
コンパイル時間 3,655 ms
コンパイル使用メモリ 204,896 KB
実行使用メモリ 22,820 KB
最終ジャッジ日時 2023-08-19 12:56:02
合計ジャッジ時間 7,867 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,504 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 169 ms
22,820 KB
testcase_08 AC 166 ms
22,564 KB
testcase_09 AC 169 ms
22,704 KB
testcase_10 AC 170 ms
22,236 KB
testcase_11 AC 170 ms
22,336 KB
testcase_12 AC 158 ms
11,488 KB
testcase_13 AC 153 ms
11,148 KB
testcase_14 AC 157 ms
11,152 KB
testcase_15 AC 156 ms
11,148 KB
testcase_16 AC 155 ms
11,336 KB
testcase_17 AC 169 ms
10,884 KB
testcase_18 AC 161 ms
10,936 KB
testcase_19 AC 162 ms
10,940 KB
testcase_20 AC 167 ms
10,884 KB
testcase_21 AC 163 ms
11,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

const ll INF = 9e18;

void dfs(vector<vector<int>> &tree, vector<ll> &dp0, vector<ll> &dp1, vector<int> &a, vector<int> &b, int now, int bf){
  dp0.at(now) = a.at(now);
  dp1.at(now) = 0;
  for(auto nx : tree.at(now)){
    if(nx == bf) continue;
    if(dp0.at(nx) == -INF) dfs(tree, dp0, dp1, a, b, nx, now);
    dp0.at(now) += max(dp0.at(nx), dp1.at(nx));
    dp1.at(now) += max(dp0.at(nx), dp1.at(nx)+b.at(now)+b.at(nx));
  }
}

int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a.at(i);
  vector<int> b(n);
  rep(i, n) cin >> b.at(i);
  vector<vector<int>> tree(n, vector<int>());
  rep(i, n-1){
    int a, b;
    cin >> a >> b;
    a--; b--;
    tree.at(a).push_back(b);
    tree.at(b).push_back(a);
  }
  vector<ll> dp0(n, -INF), dp1(n, -INF);
  dfs(tree, dp0, dp1, a, b, 0, -1);
  cout << max(dp0.at(0), dp1.at(0)) << endl;
  return 0;
}
0