結果

問題 No.1221 木 *= 3
ユーザー chielochielo
提出日時 2020-09-04 22:08:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 2,069 bytes
コンパイル時間 2,289 ms
コンパイル使用メモリ 208,000 KB
実行使用メモリ 36,940 KB
最終ジャッジ日時 2023-08-17 15:44:02
合計ジャッジ時間 5,212 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,836 KB
testcase_01 AC 5 ms
13,860 KB
testcase_02 AC 5 ms
13,848 KB
testcase_03 AC 6 ms
13,808 KB
testcase_04 AC 5 ms
13,920 KB
testcase_05 AC 5 ms
13,864 KB
testcase_06 AC 5 ms
13,940 KB
testcase_07 AC 115 ms
36,940 KB
testcase_08 AC 115 ms
36,232 KB
testcase_09 AC 113 ms
36,536 KB
testcase_10 AC 117 ms
36,536 KB
testcase_11 AC 116 ms
36,552 KB
testcase_12 AC 83 ms
23,900 KB
testcase_13 AC 91 ms
23,900 KB
testcase_14 AC 94 ms
23,912 KB
testcase_15 AC 91 ms
23,900 KB
testcase_16 AC 90 ms
24,072 KB
testcase_17 AC 107 ms
23,564 KB
testcase_18 AC 111 ms
23,432 KB
testcase_19 AC 112 ms
23,520 KB
testcase_20 AC 111 ms
23,528 KB
testcase_21 AC 109 ms
23,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

const int maxn = 2e5 + 3;
using ll = long long;

int n, a[maxn], b[maxn], p[maxn];
std::vector<int> E[maxn], c[maxn];
ll dp[maxn][2][2];

void dfs(int u) {
    for(auto &&v : E[u]) {
        if(v == p[u])
            continue;
        p[v] = u;
        dfs(v);
        c[u].push_back(v);
    }
    ll au = a[u], bu = b[u];
    {
        {
            ll &ansP = dp[u][1][1], &ansN = dp[u][1][0], dps = 0;
            std::vector<ll> tr;
            for(auto &&v : c[u]) {
                dps += dp[v][0][1];
                tr.push_back(dp[v][1][1] - dp[v][0][1]);
            }
            std::sort(tr.rbegin(), tr.rend());
            ll dpp = 0;
            ansP = dps + dpp + bu * 1ll;
            ansN = dps + dpp + bu * 0ll;
            for(int j = 0; j < c[u].size(); ++j) {
                dpp += tr[j];
                ansP = std::max(ansP, dps + dpp + bu * ll(j + 2ll));
                ansN = std::max(ansN, dps + dpp + bu * ll(j + 1ll));
            }
        }
        {
            ll &ansP = dp[u][0][1], &ansN = dp[u][0][0], dps = 0;
            std::vector<ll> tr;
            for(auto &&v : c[u]) {
                dps += dp[v][0][0];
                tr.push_back(dp[v][1][0] - dp[v][0][0]);
            }
            std::sort(tr.rbegin(), tr.rend());
            ll dpp = 0;
            ansP = dps + dpp + au;
            ansN = dps + dpp + au;
            for(int j = 0; j < c[u].size(); ++j) {
                dpp += tr[j];
                ansP = std::max(ansP, dps + dpp + au);
                ansN = std::max(ansN, dps + dpp + au);
            }
        }
    }
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i)
        scanf("%d", a + i);
    for(int i = 1; i <= n; ++i)
        scanf("%d", b + i);
    for(int i = 0; i < n - 1; ++i) {
        int u, v;
        scanf("%d%d", &u, &v);
        E[u].push_back(v);
        E[v].push_back(u);
    }
    dfs(1);
    ll ans = dp[1][0][0];
    for(int i = 0; i < 2; ++i)
        ans = std::max(ans, dp[1][i][0]);
    printf("%lld\n", ans);
    return 0;
}
0