結果

問題 No.1221 木 *= 3
ユーザー hedwig100hedwig100
提出日時 2020-09-04 22:18:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,296 bytes
コンパイル時間 1,667 ms
コンパイル使用メモリ 173,772 KB
実行使用メモリ 23,520 KB
最終ジャッジ日時 2023-08-17 16:44:28
合計ジャッジ時間 4,597 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
12,568 KB
testcase_01 AC 12 ms
12,408 KB
testcase_02 AC 12 ms
12,480 KB
testcase_03 AC 13 ms
12,568 KB
testcase_04 AC 12 ms
12,464 KB
testcase_05 AC 12 ms
12,360 KB
testcase_06 AC 13 ms
12,404 KB
testcase_07 AC 87 ms
23,324 KB
testcase_08 AC 88 ms
23,080 KB
testcase_09 AC 90 ms
23,520 KB
testcase_10 AC 91 ms
23,140 KB
testcase_11 AC 88 ms
23,056 KB
testcase_12 AC 80 ms
15,952 KB
testcase_13 AC 77 ms
15,976 KB
testcase_14 AC 79 ms
15,952 KB
testcase_15 AC 80 ms
15,852 KB
testcase_16 AC 80 ms
15,904 KB
testcase_17 AC 90 ms
15,640 KB
testcase_18 AC 87 ms
15,740 KB
testcase_19 AC 92 ms
15,688 KB
testcase_20 AC 92 ms
15,744 KB
testcase_21 AC 92 ms
15,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i ++)
#define irep(i,n) for (int i = (int)(n) - 1;i >= 0;--i)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;// = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

int N = 100010;
vector<ll> A(N),B(N);
vector<vector<int>> G(N);
vector<vector<ll>> dp(N,vector<ll>(2,-1));

void dfs(int v,int p) {
    for (int e:G[v]) {
        if (p == e) continue;
        dfs(e,v);
    }

    // v消す
    ll res = A[v];
    for (int e:G[v]) {
        if (p == e) continue;
        res += max(dp[e][0],dp[e][1]);
    }
    dp[v][0] = res;

    // v消さない
    res = 0;
    for (int e:G[v]) {
        if (p == e) continue;
        res += max(dp[e][0],dp[e][1] + B[v] + B[e]);
    }
    dp[v][1] = res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> N;
    rep(i,N) cin >> A[i];
    rep(i,N) cin >> B[i];
    rep(i,N - 1) {
        int a,b; cin >> a >> b;
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(0,-1);
    cout << max(dp[0][0],dp[0][1]) << '\n';
    return 0;
}
0