結果

問題 No.1098 LCAs
ユーザー MisterMister
提出日時 2020-08-04 03:37:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 1,750 bytes
コンパイル時間 933 ms
コンパイル使用メモリ 94,508 KB
実行使用メモリ 33,068 KB
最終ジャッジ日時 2023-10-11 22:44:50
合計ジャッジ時間 6,935 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 1 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 3 ms
4,356 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,352 KB
testcase_18 AC 162 ms
17,716 KB
testcase_19 AC 161 ms
17,832 KB
testcase_20 AC 160 ms
17,700 KB
testcase_21 AC 164 ms
17,756 KB
testcase_22 AC 158 ms
17,688 KB
testcase_23 AC 109 ms
17,820 KB
testcase_24 AC 108 ms
17,824 KB
testcase_25 AC 100 ms
17,964 KB
testcase_26 AC 108 ms
18,016 KB
testcase_27 AC 103 ms
17,816 KB
testcase_28 AC 192 ms
31,692 KB
testcase_29 AC 190 ms
33,068 KB
testcase_30 AC 185 ms
31,428 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]; }
};

using lint = long long;

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;
        graph.span(false, --u, --v);
    }

    std::vector<lint> ans(n, 0);
    std::function<lint(int, int)> dfs =
        [&](int v, int p) -> lint {
        lint sz = 1;

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

            lint csz = dfs(u, v);
            sz += csz;
            ans[v] -= csz * csz;
        }
        ans[v] += sz * sz;
        return sz;
    };
    dfs(0, -1);

    for (auto a : ans) std::cout << a << "\n";
}

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

    solve();

    return 0;
}
0