結果

問題 No.1221 木 *= 3
ユーザー yuuiyuui
提出日時 2020-09-04 22:50:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 2,066 ms
コンパイル使用メモリ 203,648 KB
実行使用メモリ 17,024 KB
最終ジャッジ日時 2024-05-05 03:24:11
合計ジャッジ時間 5,896 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,632 KB
testcase_01 AC 4 ms
5,504 KB
testcase_02 AC 4 ms
5,504 KB
testcase_03 AC 4 ms
5,504 KB
testcase_04 AC 3 ms
5,504 KB
testcase_05 AC 4 ms
5,632 KB
testcase_06 AC 4 ms
5,504 KB
testcase_07 AC 181 ms
17,024 KB
testcase_08 AC 176 ms
16,896 KB
testcase_09 AC 177 ms
17,024 KB
testcase_10 AC 180 ms
17,024 KB
testcase_11 AC 179 ms
16,896 KB
testcase_12 AC 170 ms
12,176 KB
testcase_13 AC 161 ms
12,176 KB
testcase_14 AC 168 ms
12,172 KB
testcase_15 AC 167 ms
12,176 KB
testcase_16 AC 166 ms
12,180 KB
testcase_17 AC 186 ms
11,904 KB
testcase_18 AC 176 ms
11,904 KB
testcase_19 AC 182 ms
11,904 KB
testcase_20 AC 180 ms
11,904 KB
testcase_21 AC 180 ms
12,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const ll INF = ll(1e18) + 5;
ll dp[int(1e5) + 5][2];
ll a[int(1e5) + 5], b[int(1e5) + 5];
vector<vector<int>> tree(int(1e5) + 5);

void dfs(int t, int p) {
    for (int child : tree[t]) {
        if (child == p) {
            continue;
        }
        dfs(child, t);
    }
    //消された場合
    dp[t][0] = a[t];
    //消されずに残った場合
    dp[t][1] = 0;
    for (int child : tree[t]) {
        if (child == p) {
            continue;
        }
        if (dp[child][0] > dp[child][1]) {
            dp[t][0] += dp[child][0];
        } else {
            dp[t][0] += dp[child][1];
        }
        if (dp[child][0] > dp[child][1] + b[t] + b[child]) {
            dp[t][1] += dp[child][0];
        } else {
            dp[t][1] += dp[child][1] + b[t] + b[child];
        }
    }
}

int main() {
    int N;
    cin >> N;
    rep(i, N) {
        cin >> a[i];
    }
    rep(i, N) {
        cin >> b[i];
    }
    rep(i, N - 1) {
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }
    dfs(0, -1);
    cout << max(dp[0][0], dp[0][1]) << endl;

    return 0;
}
0