結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー MisterMister
提出日時 2021-03-05 21:40:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,631 bytes
コンパイル時間 910 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 22,656 KB
最終ジャッジ日時 2024-04-16 09:05:40
合計ジャッジ時間 3,277 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 77 ms
9,728 KB
testcase_04 AC 72 ms
9,704 KB
testcase_05 AC 76 ms
9,728 KB
testcase_06 AC 71 ms
9,856 KB
testcase_07 AC 72 ms
9,728 KB
testcase_08 AC 41 ms
7,808 KB
testcase_09 AC 18 ms
6,940 KB
testcase_10 AC 20 ms
6,944 KB
testcase_11 AC 11 ms
6,944 KB
testcase_12 AC 32 ms
6,940 KB
testcase_13 AC 42 ms
7,296 KB
testcase_14 AC 41 ms
7,680 KB
testcase_15 AC 28 ms
6,940 KB
testcase_16 AC 4 ms
6,940 KB
testcase_17 AC 4 ms
6,944 KB
testcase_18 AC 58 ms
9,216 KB
testcase_19 AC 10 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 29 ms
6,944 KB
testcase_22 AC 24 ms
6,940 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 3 ms
6,944 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 3 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 4 ms
6,944 KB
testcase_30 AC 4 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 4 ms
6,944 KB
testcase_33 AC 15 ms
7,680 KB
testcase_34 AC 73 ms
22,656 KB
testcase_35 AC 27 ms
11,392 KB
testcase_36 AC 6 ms
6,948 KB
testcase_37 AC 46 ms
11,132 KB
testcase_38 AC 41 ms
10,536 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 AC 1 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp"

#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;

    Edge() = default;
    Edge(int src, int dst, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

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

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

    void span(bool direct, int src, int dst, Cost cost = 1) {
        (*this)[src].emplace_back(src, dst, cost);
        if (!direct) (*this)[dst].emplace_back(dst, src, cost);
    }
};
#line 2 "main.cpp"
#include <iostream>
#line 4 "main.cpp"

using namespace std;
using lint = long long;

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

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

    lint ans = 0;
    auto dfs = [&](auto&& f, int v, int p) -> lint {
        vector<lint> cz;
        lint sz = 1;

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

            lint c = f(f, u, v);
            cz.push_back(c);
            sz += c;
        }

        cz.push_back(n - sz);

        ans += n;
        for (auto c : cz) ans += c * (n - c);

        return sz;
    };
    dfs(dfs, 0, -1);

    cout << ans << "\n";
}

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

    solve();

    return 0;
}
0