結果

問題 No.1221 木 *= 3
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-09-05 03:09:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 1,657 ms
コンパイル使用メモリ 175,440 KB
実行使用メモリ 16,896 KB
最終ジャッジ日時 2024-05-05 08:43:08
合計ジャッジ時間 6,046 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 163 ms
16,896 KB
testcase_08 AC 151 ms
16,640 KB
testcase_09 AC 156 ms
16,768 KB
testcase_10 AC 158 ms
16,640 KB
testcase_11 AC 159 ms
16,640 KB
testcase_12 AC 154 ms
10,652 KB
testcase_13 AC 143 ms
10,648 KB
testcase_14 AC 147 ms
10,776 KB
testcase_15 AC 146 ms
10,772 KB
testcase_16 AC 142 ms
10,648 KB
testcase_17 AC 158 ms
10,368 KB
testcase_18 AC 154 ms
10,368 KB
testcase_19 AC 153 ms
10,368 KB
testcase_20 AC 164 ms
10,368 KB
testcase_21 AC 150 ms
10,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.09.05 03:09:52
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int n;
vector<vector<int>> g;
vector<ll> a,b;

pair<ll,ll> dfs(int v, int p = -1) {
    pair<ll,ll> ret = {a[v],0};
    for(int u : g[v]) {
        if (u == p) continue;
        auto t = dfs(u,v);
        ret.first += max(t.first,t.second);
        ret.second += max(b[u] + t.second + b[v], t.first);
    }
    return ret;
}
int main() {
    cin >> n;
    a.resize(n);
    b.resize(n);
    g.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> b[i];
    }
    for (int i = 0; i < n-1; i++) {
        int u,v;cin >> u >> v;
        u--;v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    auto ans = dfs(0);
    cout << max(ans.first,ans.second) << endl;
    return 0;
}
0