結果

問題 No.1221 木 *= 3
ユーザー outlineoutline
提出日時 2021-01-01 16:04:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 78 ms / 2,000 ms
コード長 2,128 bytes
コンパイル時間 1,340 ms
コンパイル使用メモリ 142,976 KB
実行使用メモリ 21,632 KB
最終ジャッジ日時 2024-04-19 12:38:22
合計ジャッジ時間 4,580 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 76 ms
21,632 KB
testcase_08 AC 74 ms
21,248 KB
testcase_09 AC 78 ms
21,376 KB
testcase_10 AC 76 ms
21,376 KB
testcase_11 AC 75 ms
21,376 KB
testcase_12 AC 68 ms
15,516 KB
testcase_13 AC 69 ms
15,388 KB
testcase_14 AC 68 ms
15,388 KB
testcase_15 AC 66 ms
15,332 KB
testcase_16 AC 69 ms
15,512 KB
testcase_17 AC 72 ms
15,104 KB
testcase_18 AC 71 ms
15,104 KB
testcase_19 AC 74 ms
15,104 KB
testcase_20 AC 75 ms
14,976 KB
testcase_21 AC 77 ms
15,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

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

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

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; ++i)  cin >> a[i];
    for(int i = 0; i < n; ++i)  cin >> b[i];
    vector<vector<int>> graph(n);
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        graph[u].emplace_back(v);
        graph[v].emplace_back(u);
    }

    constexpr ll inf = 1e+17;
    vector<vector<ll>> dp(n, vector<ll>(2, -inf));
    auto func = [&](auto&& self, int v = 0, int i = 0, int par = -1) -> ll {
        if(dp[v][i] != -inf)    return dp[v][i];
        ll res = 0;
        if(i == 0){
            res = a[v];
            for(int child : graph[v]){
                if(child == par)    continue;
                res += max(self(self, child, 0, v), self(self, child, 1, v));
            }
        }
        else{
            for(int child : graph[v]){
                if(child == par)    continue;
                res += max(self(self, child, 0, v), self(self, child, 1, v) + b[v] + b[child]);
            }
        }
        return dp[v][i] = res;
    };
    
    cout << max(func(func), func(func, 0, 1)) << endl;

    return 0;
}
0