結果
| 問題 | No.763 Noelちゃんと木遊び | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-08-04 02:15:20 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 61 ms / 2,000 ms | 
| コード長 | 1,761 bytes | 
| コンパイル時間 | 802 ms | 
| コンパイル使用メモリ | 89,532 KB | 
| 実行使用メモリ | 18,048 KB | 
| 最終ジャッジ日時 | 2025-03-22 10:38:19 | 
| 合計ジャッジ時間 | 2,727 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 22 | 
ソースコード
#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;
}
            
            
            
        