結果

問題 No.1221 木 *= 3
ユーザー kyoprounokyoprouno
提出日時 2020-09-06 00:16:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 2,000 ms
コード長 1,211 bytes
コンパイル時間 2,750 ms
コンパイル使用メモリ 179,020 KB
実行使用メモリ 18,320 KB
最終ジャッジ日時 2023-08-19 13:17:32
合計ジャッジ時間 6,138 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 173 ms
18,320 KB
testcase_08 AC 171 ms
17,800 KB
testcase_09 AC 172 ms
18,012 KB
testcase_10 AC 172 ms
18,104 KB
testcase_11 AC 174 ms
18,060 KB
testcase_12 AC 158 ms
11,992 KB
testcase_13 AC 154 ms
12,116 KB
testcase_14 AC 155 ms
12,104 KB
testcase_15 AC 156 ms
12,080 KB
testcase_16 AC 156 ms
12,116 KB
testcase_17 AC 176 ms
11,780 KB
testcase_18 AC 169 ms
11,700 KB
testcase_19 AC 171 ms
11,764 KB
testcase_20 AC 171 ms
11,840 KB
testcase_21 AC 170 ms
11,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

const ll mod=1e9+7;

ll n;

vector<vector<int>> to;
vector<ll> a,b,dp0,dp1;

void dfs(int v,int p=-1){
    dp0[v]=a[v];
    for(auto x:to[v]){
        if(x==p) continue;
        dfs(x,v);
        dp0[v]+=max(dp0[x],dp1[x]);
        dp1[v]+=max(dp0[x],dp1[x]+b[x]+b[v]);
    }
    return ;
}

int main(){
    cin >> n;
    to=vector<vector<int>>(n);
    a=vector<ll>(n);
    b=vector<ll>(n);
    rep(i,n){
        cin >> a[i];
    }
    rep(i,n){
        cin >> b[i];
    }
    rep(i,n-1){
        ll u,v;
        cin >> u >> v;
        u--; v--;
        to[u].push_back(v);
        to[v].push_back(u);
    }
    dp0=vector<ll>(n);
    dp1=vector<ll>(n);
    dfs(0);
    cout << max(dp0[0],dp1[0]) << endl;
    return 0;
}
0