結果

問題 No.1221 木 *= 3
ユーザー MisterMister
提出日時 2020-09-04 21:40:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 70 ms / 2,000 ms
コード長 1,870 bytes
コンパイル時間 863 ms
コンパイル使用メモリ 82,008 KB
実行使用メモリ 14,980 KB
最終ジャッジ日時 2023-08-17 15:14:53
合計ジャッジ時間 3,084 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 68 ms
14,872 KB
testcase_08 AC 68 ms
14,936 KB
testcase_09 AC 67 ms
14,968 KB
testcase_10 AC 70 ms
14,932 KB
testcase_11 AC 68 ms
14,980 KB
testcase_12 AC 59 ms
11,288 KB
testcase_13 AC 60 ms
11,240 KB
testcase_14 AC 65 ms
11,284 KB
testcase_15 AC 60 ms
11,192 KB
testcase_16 AC 62 ms
11,284 KB
testcase_17 AC 68 ms
11,272 KB
testcase_18 AC 67 ms
11,180 KB
testcase_19 AC 68 ms
11,184 KB
testcase_20 AC 68 ms
11,236 KB
testcase_21 AC 66 ms
11,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

using lint = long long;

void solve() {
    int n;
    std::cin >> n;

    std::vector<lint> xs(n), ys(n);
    for (auto& x : xs) std::cin >> x;
    for (auto& y : ys) std::cin >> y;

    Graph<> graph(n);
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        std::cin >> u >> v;
        graph.span(false, --u, --v);
    }

    auto dfs = [&](auto&& f, int v, int p)
        -> std::pair<lint, lint> {
        lint s0 = xs[v], s1 = 0;

        for (auto e : graph[v]) {
            int u = e.dst;
            if (u == p) continue;

            auto [t0, t1] = f(f, u, v);
            s0 += std::max(t0, t1);
            s1 += std::max(t0, t1 + ys[v] + ys[u]);
        }
        return std::make_pair(s0, s1);
    };

    auto [s0, s1] = dfs(dfs, 0, -1);
    std::cout << std::max(s0, s1) << "\n";
}

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

    solve();

    return 0;
}
0