結果

問題 No.1221 木 *= 3
ユーザー firiexpfiriexp
提出日時 2020-09-04 22:18:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 1,450 bytes
コンパイル時間 1,202 ms
コンパイル使用メモリ 112,576 KB
実行使用メモリ 12,848 KB
最終ジャッジ日時 2024-05-04 23:04:05
合計ジャッジ時間 3,761 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 81 ms
12,196 KB
testcase_08 AC 87 ms
12,416 KB
testcase_09 AC 85 ms
12,416 KB
testcase_10 AC 82 ms
12,428 KB
testcase_11 AC 87 ms
12,416 KB
testcase_12 AC 72 ms
12,720 KB
testcase_13 AC 70 ms
12,720 KB
testcase_14 AC 72 ms
12,724 KB
testcase_15 AC 71 ms
12,716 KB
testcase_16 AC 74 ms
12,848 KB
testcase_17 AC 90 ms
12,544 KB
testcase_18 AC 88 ms
12,544 KB
testcase_19 AC 87 ms
12,416 KB
testcase_20 AC 91 ms
12,416 KB
testcase_21 AC 89 ms
12,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    cin >> n;
    vector<int> A(n), B(n), num(n);
    for (auto &&i : A) scanf("%d", &i);
    for (auto &&i : B) scanf("%d", &i);
    vector<ll> dp1(n, 0), dp2(n, 0);
    vector<vector<int>> G(n);
    for (int i = 0; i < n-1; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    deque<int> Q;
    {
        stack<int> s;
        int cnt = 0;
        vector<int> visited(n, 0);
        s.emplace(0);
        while(!s.empty()){
            int a = s.top(); s.pop();
            visited[a]++;
            num[a] = cnt++;
            Q.emplace_front(a);
            for (auto &&i : G[a]) {
                if(!visited[i]) s.emplace(i);
            }
        }
    }
    for (auto &&i : Q) {
        dp1[i] = A[i];
        for (auto &&j : G[i]) {
            if(num[i] < num[j]) {
                dp1[i] += max(dp1[j], dp2[j]);
                dp2[i] += max(dp1[j], dp2[j]+B[i]+B[j]);
            }
        }
    }
    cout << max(dp1[0], dp2[0]) << "\n";
    return 0;
}
0