結果

問題 No.1221 木 *= 3
ユーザー sapphire__15sapphire__15
提出日時 2021-11-17 18:24:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 828 bytes
コンパイル時間 606 ms
コンパイル使用メモリ 74,980 KB
実行使用メモリ 16,564 KB
最終ジャッジ日時 2023-08-25 12:58:03
合計ジャッジ時間 5,644 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,844 KB
testcase_01 AC 2 ms
6,048 KB
testcase_02 AC 2 ms
5,896 KB
testcase_03 AC 2 ms
5,964 KB
testcase_04 AC 2 ms
5,968 KB
testcase_05 AC 2 ms
5,844 KB
testcase_06 AC 1 ms
6,096 KB
testcase_07 AC 133 ms
16,564 KB
testcase_08 AC 130 ms
16,168 KB
testcase_09 AC 133 ms
16,456 KB
testcase_10 AC 136 ms
16,268 KB
testcase_11 AC 138 ms
16,308 KB
testcase_12 AC 130 ms
10,524 KB
testcase_13 AC 119 ms
10,492 KB
testcase_14 AC 123 ms
10,544 KB
testcase_15 AC 122 ms
10,632 KB
testcase_16 AC 123 ms
10,640 KB
testcase_17 AC 138 ms
11,308 KB
testcase_18 AC 126 ms
11,240 KB
testcase_19 AC 130 ms
11,292 KB
testcase_20 AC 131 ms
11,292 KB
testcase_21 AC 130 ms
11,232 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

const int MAX_N = 100100;
int a[MAX_N];
int b[MAX_N];
vector<int> edge[MAX_N];
long long dp1[MAX_N]; // 消さないときの最大値
long long dp2[MAX_N]; // 消すときの最大値

void dfs(int crt, int parent = -1) {
    for(auto &i : edge[crt]) {
        if(i == parent) continue;
        dfs(i, crt);
        dp1[crt] += max(dp1[i] + b[crt] + b[i], dp2[i]);
        dp2[crt] += max(dp1[i], dp2[i]);
    }
    dp2[crt] += a[crt];
}

int main() {
    int N; cin >> N;
    for(int i = 0; i < N; i++) cin >> a[i];
    for(int i = 0; i < N; i++) cin >> b[i];
    for(int i = 0; i < N-1; i++) {
        int u, v; cin >> u >> v;
        --u; --v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(0);
    cout << max(dp1[0], dp2[0]) << endl;
}
0