結果

問題 No.1221 木 *= 3
ユーザー yuuiyuui
提出日時 2020-09-04 22:50:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 177 ms / 2,000 ms
コード長 1,273 bytes
コンパイル時間 2,086 ms
コンパイル使用メモリ 202,524 KB
実行使用メモリ 16,900 KB
最終ジャッジ日時 2023-08-17 20:29:52
合計ジャッジ時間 5,970 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,224 KB
testcase_01 AC 3 ms
5,444 KB
testcase_02 AC 4 ms
5,244 KB
testcase_03 AC 2 ms
5,132 KB
testcase_04 AC 4 ms
5,340 KB
testcase_05 AC 3 ms
5,120 KB
testcase_06 AC 3 ms
5,180 KB
testcase_07 AC 172 ms
16,768 KB
testcase_08 AC 170 ms
16,788 KB
testcase_09 AC 170 ms
16,900 KB
testcase_10 AC 172 ms
16,816 KB
testcase_11 AC 175 ms
16,760 KB
testcase_12 AC 158 ms
11,996 KB
testcase_13 AC 150 ms
11,992 KB
testcase_14 AC 156 ms
11,956 KB
testcase_15 AC 156 ms
12,084 KB
testcase_16 AC 154 ms
11,940 KB
testcase_17 AC 177 ms
11,792 KB
testcase_18 AC 171 ms
11,744 KB
testcase_19 AC 170 ms
11,744 KB
testcase_20 AC 173 ms
11,968 KB
testcase_21 AC 175 ms
11,688 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