結果

問題 No.1221 木 *= 3
ユーザー roarisroaris
提出日時 2020-09-05 11:35:42
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 1,021 bytes
コンパイル時間 1,907 ms
コンパイル使用メモリ 202,708 KB
実行使用メモリ 18,652 KB
最終ジャッジ日時 2023-08-18 17:37:56
合計ジャッジ時間 4,933 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,096 KB
testcase_01 AC 3 ms
5,984 KB
testcase_02 AC 2 ms
5,892 KB
testcase_03 AC 3 ms
5,988 KB
testcase_04 AC 2 ms
5,892 KB
testcase_05 AC 2 ms
5,888 KB
testcase_06 AC 2 ms
5,992 KB
testcase_07 AC 75 ms
18,484 KB
testcase_08 AC 74 ms
18,336 KB
testcase_09 AC 74 ms
18,652 KB
testcase_10 AC 75 ms
18,280 KB
testcase_11 AC 76 ms
18,356 KB
testcase_12 AC 61 ms
12,492 KB
testcase_13 AC 60 ms
12,444 KB
testcase_14 AC 63 ms
12,476 KB
testcase_15 AC 62 ms
12,344 KB
testcase_16 AC 63 ms
12,492 KB
testcase_17 AC 70 ms
12,568 KB
testcase_18 AC 70 ms
12,612 KB
testcase_19 AC 73 ms
12,864 KB
testcase_20 AC 73 ms
12,636 KB
testcase_21 AC 74 ms
12,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
#define int long long

int N;
int a[100010], b[100010];
vector<int> G[100010];
int memo[2][100010];

int dfs(int v, int pv, int f) {//f:0 vを消す, f:1 vを消さない
    if (memo[f][v]!=-1) return memo[f][v];
    int res = f==0 ? a[v]:0;
    if (f==0) {
        for (auto& nv:G[v]) {
            if (nv==pv) continue;
            res += max(dfs(nv, v, 0), dfs(nv, v, 1));
        }
    }
    else {
        for (auto& nv:G[v]) {
            if (nv==pv) continue;
            res += max(dfs(nv, v, 0), dfs(nv, v, 1)+b[v]+b[nv]);
        }
    }
    return memo[f][v] = res;
}

signed main() {
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> N;
    rep(i, N) cin >> a[i];
    rep(i, N) cin >> b[i];
    rep(i, N-1) {
        int u, v; cin >> u >> v;
        G[u-1].pb(v-1);
        G[v-1].pb(u-1);
    }
    rep(i, 2) rep(j, N) memo[i][j] = -1;
    cout << max(dfs(0, -1, 0), dfs(0, -1, 1)) << endl;
}
0