結果

問題 No.1221 木 *= 3
ユーザー mgingin142857mgingin142857
提出日時 2020-09-04 22:07:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 80 ms / 2,000 ms
コード長 1,343 bytes
コンパイル時間 1,423 ms
コンパイル使用メモリ 166,512 KB
実行使用メモリ 18,552 KB
最終ジャッジ日時 2023-08-17 15:43:33
合計ジャッジ時間 3,989 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,976 KB
testcase_01 AC 3 ms
6,816 KB
testcase_02 AC 4 ms
6,976 KB
testcase_03 AC 3 ms
6,924 KB
testcase_04 AC 3 ms
6,908 KB
testcase_05 AC 4 ms
6,960 KB
testcase_06 AC 3 ms
6,848 KB
testcase_07 AC 79 ms
18,552 KB
testcase_08 AC 80 ms
18,356 KB
testcase_09 AC 76 ms
18,464 KB
testcase_10 AC 80 ms
18,248 KB
testcase_11 AC 80 ms
18,356 KB
testcase_12 AC 65 ms
11,976 KB
testcase_13 AC 64 ms
12,180 KB
testcase_14 AC 64 ms
12,068 KB
testcase_15 AC 67 ms
12,060 KB
testcase_16 AC 65 ms
12,120 KB
testcase_17 AC 73 ms
12,184 KB
testcase_18 AC 76 ms
12,172 KB
testcase_19 AC 75 ms
12,120 KB
testcase_20 AC 76 ms
12,060 KB
testcase_21 AC 75 ms
12,108 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

int n;
vector<int> E[101010];
ll dp[101010][2];
ll a[101010], b[101010];

void dfs(int cu, int pa = -1) {
    dp[cu][0] = a[cu];
    dp[cu][1] = 0;
    for (ll to : E[cu])
        if (to != pa) {
            dfs(to, cu);
            dp[cu][0] += max(dp[to][0], dp[to][1]);
            dp[cu][1] += max(dp[to][0], dp[to][1] + b[cu] + b[to]);
        }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int i, j;
    cin >> n;
    for (i = 0; i < n; i++) cin >> a[i];
    for (i = 0; i < n; i++) cin >> b[i];
    for (i = 0; i < n - 1; i++) {
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        E[u].pb(v);
        E[v].pb(u);
    }
    dfs(0, -1);
    cout << max(dp[0][0], dp[0][1]) << endl;
}
0