結果

問題 No.763 Noelちゃんと木遊び
ユーザー MisterMister
提出日時 2020-08-04 02:15:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 1,761 bytes
コンパイル時間 923 ms
コンパイル使用メモリ 93,700 KB
実行使用メモリ 17,808 KB
最終ジャッジ日時 2023-10-11 21:42:36
合計ジャッジ時間 3,953 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
17,808 KB
testcase_01 AC 16 ms
5,072 KB
testcase_02 AC 38 ms
8,036 KB
testcase_03 AC 25 ms
6,380 KB
testcase_04 AC 19 ms
5,720 KB
testcase_05 AC 23 ms
6,420 KB
testcase_06 AC 46 ms
9,012 KB
testcase_07 AC 47 ms
9,096 KB
testcase_08 AC 25 ms
6,712 KB
testcase_09 AC 16 ms
5,252 KB
testcase_10 AC 8 ms
4,348 KB
testcase_11 AC 46 ms
9,344 KB
testcase_12 AC 44 ms
8,420 KB
testcase_13 AC 44 ms
8,492 KB
testcase_14 AC 37 ms
8,180 KB
testcase_15 AC 25 ms
6,512 KB
testcase_16 AC 6 ms
4,352 KB
testcase_17 AC 25 ms
6,488 KB
testcase_18 AC 49 ms
9,292 KB
testcase_19 AC 44 ms
8,484 KB
testcase_20 AC 43 ms
8,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>

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]; }
};

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

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

    std::function<std::pair<int, int>(int, int)> dfs =
        [&](int v, int p) -> std::pair<int, int> {
        std::pair<int, int> ret(0, 1);

        for (auto e : graph[v]) {
            int u = e.dst;
            if (u == p) continue;
            auto [z, o] = dfs(u, v);
            ret.first += std::max(z, o);
            ret.second += z;
        }

        return ret;
    };

    auto p = dfs(0, -1);
    std::cout << std::max(p.first, p.second) << "\n";
}

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

    solve();

    return 0;
}
0