結果

問題 No.1221 木 *= 3
ユーザー sahiyasahiya
提出日時 2020-12-18 16:35:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,808 bytes
コンパイル時間 1,844 ms
コンパイル使用メモリ 121,952 KB
実行使用メモリ 20,924 KB
最終ジャッジ日時 2023-10-21 08:09:59
合計ジャッジ時間 7,869 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,356 KB
testcase_01 AC 4 ms
8,356 KB
testcase_02 AC 4 ms
8,356 KB
testcase_03 AC 4 ms
8,356 KB
testcase_04 AC 4 ms
8,356 KB
testcase_05 AC 4 ms
8,356 KB
testcase_06 AC 4 ms
8,356 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
権限があれば一括ダウンロードができます

ソースコード

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 res;
}
0