結果

問題 No.1221 木 *= 3
ユーザー chielochielo
提出日時 2020-09-04 22:08:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 2,069 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 204,380 KB
最終ジャッジ日時 2025-01-14 05:48:38
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 18
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
main.cpp:60:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   60 |         scanf("%d", a + i);
      |         ~~~~~^~~~~~~~~~~~~
main.cpp:62:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   62 |         scanf("%d", b + i);
      |         ~~~~~^~~~~~~~~~~~~
main.cpp:65:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   65 |         scanf("%d%d", &u, &v);
      |         ~~~~~^~~~~~~~~~~~~~~~

ソースコード

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