結果

問題 No.1221 木 *= 3
ユーザー IKyoproIKyopro
提出日時 2020-09-04 21:58:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 97 ms / 2,000 ms
コード長 1,123 bytes
コンパイル時間 2,110 ms
コンパイル使用メモリ 209,344 KB
実行使用メモリ 20,472 KB
最終ジャッジ日時 2023-08-17 15:36:06
合計ジャッジ時間 4,923 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 86 ms
20,472 KB
testcase_08 AC 83 ms
20,212 KB
testcase_09 AC 84 ms
20,420 KB
testcase_10 AC 89 ms
20,280 KB
testcase_11 AC 84 ms
20,168 KB
testcase_12 AC 74 ms
16,140 KB
testcase_13 AC 74 ms
16,088 KB
testcase_14 AC 74 ms
16,024 KB
testcase_15 AC 74 ms
16,088 KB
testcase_16 AC 73 ms
15,948 KB
testcase_17 AC 83 ms
15,776 KB
testcase_18 AC 87 ms
15,716 KB
testcase_19 AC 95 ms
15,776 KB
testcase_20 AC 97 ms
15,796 KB
testcase_21 AC 95 ms
15,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<ll> A(N),B(N);
    for(auto& x:A) cin >> x;
    for(auto& x:B) cin >> x;
    vvec<int> g(N);
    for(int i=0;i<N-1;i++){
        int a,b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    ll inf = 1e18;
    vvec<ll> dp(N,vec<ll>(2,0));
    auto dfs = [&](auto&& self,int cur,int par)->void{
        for(auto& to:g[cur]) if(to!=par){
            self(self,to,cur);
            dp[cur][0] += max(dp[to][0],dp[to][1]);
            dp[cur][1] += max(dp[to][0],dp[to][1]+B[to]+B[cur]);
        }
        dp[cur][0] += A[cur];
    };
    dfs(dfs,0,-1);
    cout << max(dp[0][0],dp[0][1]) << "\n";
}
0