結果

問題 No.806 木を道に
ユーザー Tiramister
提出日時 2019-03-22 22:51:48
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,053 bytes
コンパイル時間 701 ms
コンパイル使用メモリ 74,800 KB
実行使用メモリ 10,816 KB
最終ジャッジ日時 2024-09-19 06:14:50
合計ジャッジ時間 2,367 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

template <class T>
struct Edge {
    int from, to;
    T cost;
    Edge(int from = -1, int to = -1, T cost = 1) : from(from), to(to), cost(cost){};

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

template <class T = int>
class Graph {
public:
    explicit Graph(int N = 0) : size(N) { path.resize(size); }
    void span(int u, int v, T cost = 1) { path[u].push_back(Edge<T>(u, v, cost)); }
    std::vector<Edge<T>> operator[](int v) const { return path[v]; }

    int size;
    std::vector<std::vector<Edge<T>>> path;
};

int main() {
    int N;
    std::cin >> N;

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

    int ans = 0;
    for (int v = 0; v < N; ++v) {
        ans += std::max(0, (int)tree[v].size() - 2);
    }
    std::cout << ans << std::endl;
    return 0;
}
0