結果

問題 No.1221 木 *= 3
ユーザー milanis48663220milanis48663220
提出日時 2020-09-04 22:24:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 999 bytes
コンパイル時間 2,926 ms
コンパイル使用メモリ 84,880 KB
実行使用メモリ 15,988 KB
最終ジャッジ日時 2023-08-17 16:59:02
合計ジャッジ時間 3,420 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,892 KB
testcase_01 AC 3 ms
5,904 KB
testcase_02 AC 3 ms
5,952 KB
testcase_03 AC 3 ms
5,904 KB
testcase_04 AC 3 ms
5,900 KB
testcase_05 AC 3 ms
6,012 KB
testcase_06 AC 3 ms
5,888 KB
testcase_07 AC 74 ms
15,984 KB
testcase_08 AC 74 ms
15,876 KB
testcase_09 AC 72 ms
15,988 KB
testcase_10 AC 75 ms
15,848 KB
testcase_11 AC 75 ms
15,904 KB
testcase_12 AC 62 ms
11,364 KB
testcase_13 AC 61 ms
11,420 KB
testcase_14 AC 64 ms
11,496 KB
testcase_15 AC 62 ms
11,424 KB
testcase_16 AC 63 ms
11,496 KB
testcase_17 AC 71 ms
12,464 KB
testcase_18 AC 70 ms
12,164 KB
testcase_19 AC 83 ms
12,216 KB
testcase_20 AC 72 ms
12,220 KB
testcase_21 AC 74 ms
12,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

int N;
ll a[100000];
ll b[100000];
bool used[100000];

vector<int> G[100000];
// dp1残す、dp2残さない
ll dp1[100000], dp2[100000];

const ll INF = 1e18;

void dfs(int v){
    used[v] = true;
    dp2[v] = a[v];
    ll max1 = -INF, max2 = -INF;
    for(int to : G[v]){
        if(!used[to]){
            dfs(to);
            dp1[v] += max(dp1[to]+b[to]+b[v], dp2[to]);
            dp2[v] += max(dp1[to], dp2[to]);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    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--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(0);
    cout << max(dp1[0], dp2[0]) << endl;
}
0