結果

問題 No.1221 木 *= 3
ユーザー sahiyasahiya
提出日時 2020-12-18 16:36:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,823 bytes
コンパイル時間 1,773 ms
コンパイル使用メモリ 121,300 KB
実行使用メモリ 17,160 KB
最終ジャッジ日時 2023-10-21 08:10:04
合計ジャッジ時間 4,597 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,420 KB
testcase_01 AC 3 ms
8,420 KB
testcase_02 AC 4 ms
8,420 KB
testcase_03 AC 3 ms
8,420 KB
testcase_04 AC 3 ms
8,420 KB
testcase_05 AC 3 ms
8,420 KB
testcase_06 AC 3 ms
8,420 KB
testcase_07 AC 69 ms
17,160 KB
testcase_08 AC 68 ms
16,984 KB
testcase_09 AC 68 ms
17,132 KB
testcase_10 AC 68 ms
17,012 KB
testcase_11 AC 66 ms
17,048 KB
testcase_12 AC 60 ms
12,260 KB
testcase_13 AC 54 ms
12,260 KB
testcase_14 AC 59 ms
12,260 KB
testcase_15 AC 57 ms
12,260 KB
testcase_16 AC 56 ms
12,260 KB
testcase_17 AC 67 ms
12,368 KB
testcase_18 AC 63 ms
12,368 KB
testcase_19 AC 62 ms
12,368 KB
testcase_20 AC 67 ms
12,368 KB
testcase_21 AC 63 ms
12,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef bitset<16> BS;

const ll MOD = 1E+09 + 7;
const ll INF = 1E18;
const int MAX_N = 1E+05;

int N;

ll A[MAX_N + 1], B[MAX_N + 1], dp[MAX_N + 1][2];
vector<int> G[MAX_N + 1];
ll tree(int par, int i, int exist);

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

    cin >> N;

    for (int i = 1; i <= N; i++) {
        cin >> A[i];
    }
    for (int i = 1; i <= N; i++) {
        cin >> B[i];
    }
    for (int i = 1; i <= N - 1; i++) {
        int from, to;
        cin >> from >> to;
        G[from].push_back(to);
        G[to].push_back(from);
    }
    fill(dp[1], dp[N + 1], -INF);

    /*
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
        }
    }
    */

    cout << max(tree(0, 1, 0), tree(0, 1, 1)) << "\n";

    return 0;
}

ll tree(int par, int i, int exist)
{
    if (dp[i][exist] != -INF)
        return dp[i][exist];
    ll res = 0;
    for (auto to : G[i]) {
        if (to != par) {
            if (exist) {
                res += max(tree(i, to, 0), tree(i, to, 1) + B[i] + B[to]);
            } else {
                res += max(tree(i, to, 0), tree(i, to, 1));
            }
        }
    }
    if (!exist)
        res += A[i];
    return dp[i][exist] = res;
}
0