結果

問題 No.1221 木 *= 3
ユーザー IKyoproIKyopro
提出日時 2020-09-04 21:58:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,123 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 210,000 KB
実行使用メモリ 20,608 KB
最終ジャッジ日時 2024-05-04 22:16:39
合計ジャッジ時間 4,240 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 74 ms
20,608 KB
testcase_08 AC 72 ms
20,480 KB
testcase_09 AC 71 ms
20,480 KB
testcase_10 AC 72 ms
20,352 KB
testcase_11 AC 71 ms
20,352 KB
testcase_12 AC 76 ms
16,100 KB
testcase_13 AC 61 ms
16,176 KB
testcase_14 AC 67 ms
16,144 KB
testcase_15 AC 69 ms
16,176 KB
testcase_16 AC 66 ms
16,176 KB
testcase_17 AC 75 ms
15,744 KB
testcase_18 AC 74 ms
15,872 KB
testcase_19 AC 75 ms
15,872 KB
testcase_20 AC 77 ms
15,872 KB
testcase_21 AC 76 ms
15,872 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