結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー hitonanodehitonanode
提出日時 2021-02-06 12:49:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,747 bytes
コンパイル時間 5,930 ms
コンパイル使用メモリ 212,112 KB
実行使用メモリ 14,412 KB
最終ジャッジ日時 2023-09-15 18:45:09
合計ジャッジ時間 8,448 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,536 KB
testcase_01 AC 3 ms
7,484 KB
testcase_02 AC 3 ms
7,684 KB
testcase_03 AC 58 ms
11,016 KB
testcase_04 AC 59 ms
11,108 KB
testcase_05 AC 57 ms
11,032 KB
testcase_06 AC 56 ms
11,012 KB
testcase_07 AC 58 ms
11,012 KB
testcase_08 AC 37 ms
9,808 KB
testcase_09 AC 18 ms
8,716 KB
testcase_10 AC 19 ms
8,736 KB
testcase_11 AC 12 ms
8,152 KB
testcase_12 AC 29 ms
9,236 KB
testcase_13 AC 34 ms
9,724 KB
testcase_14 AC 35 ms
9,716 KB
testcase_15 AC 28 ms
9,380 KB
testcase_16 AC 5 ms
7,648 KB
testcase_17 AC 6 ms
7,680 KB
testcase_18 AC 51 ms
10,780 KB
testcase_19 AC 10 ms
8,136 KB
testcase_20 AC 4 ms
7,648 KB
testcase_21 AC 27 ms
9,176 KB
testcase_22 AC 23 ms
8,796 KB
testcase_23 AC 5 ms
7,604 KB
testcase_24 AC 4 ms
7,572 KB
testcase_25 AC 4 ms
7,560 KB
testcase_26 AC 4 ms
7,536 KB
testcase_27 AC 3 ms
7,644 KB
testcase_28 AC 4 ms
7,444 KB
testcase_29 AC 5 ms
7,648 KB
testcase_30 AC 5 ms
7,568 KB
testcase_31 AC 4 ms
7,488 KB
testcase_32 AC 4 ms
7,556 KB
testcase_33 AC 14 ms
9,100 KB
testcase_34 AC 58 ms
14,412 KB
testcase_35 AC 25 ms
10,200 KB
testcase_36 AC 7 ms
7,968 KB
testcase_37 AC 34 ms
11,064 KB
testcase_38 AC 33 ms
10,912 KB
testcase_39 AC 3 ms
7,464 KB
testcase_40 AC 3 ms
7,484 KB
testcase_41 AC 3 ms
7,464 KB
testcase_42 AC 4 ms
7,452 KB
testcase_43 AC 3 ms
7,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// VALIDATOR
#include "testlib.h"

#include <cassert>
#include <numeric>
#include <vector>
using namespace std;

// UnionFind Tree (0-indexed), based on size of each disjoint set
struct UnionFind {
    std::vector<int> par, cou;
    UnionFind(int N = 0) : par(N), cou(N, 1) { iota(par.begin(), par.end(), 0); }
    int find(int x) { return (par[x] == x) ? x : (par[x] = find(par[x])); }
    bool unite(int x, int y) {
        x = find(x), y = find(y);
        if (x == y) return false;
        if (cou[x] < cou[y]) std::swap(x, y);
        par[y] = x, cou[x] += cou[y];
        return true;
    }
    int count(int x) { return cou[find(x)]; }
    bool same(int x, int y) { return find(x) == find(y); }
};

vector<int> to[100000];

int main(int argc, char **argv) {
    registerValidation(argc, argv);

    int N = inf.readInt(2, 100000);
    inf.readEoln();

    UnionFind uf(N);

    for (int i = 0; i < N - 1; i++) {
        int a = inf.readInt(1, N);
        inf.readSpace();
        int b = inf.readInt(1, N);
        inf.readEoln();
        a--, b--;
        uf.unite(a, b);
        to[a].push_back(b);
        to[b].push_back(a);
    }

    inf.readEof();

    if (uf.count(0) != N) {
        // NOT tree
        exit(8);
    }

    long long ret = 1LL * N * N;

    auto f = [&N](long long x) -> long long { return x * (N - x); };

    auto dfs = [&](auto self, int now, int prv) -> int {
        int subtree_size = 1;
        for (auto nxt : to[now]) {
            if (nxt != prv) {
                int tmp = self(self, nxt, now);
                ret += f(tmp);
                subtree_size += tmp;
            }
        }
        ret += f(subtree_size);
        return subtree_size;
    };
    dfs(dfs, 0, -1);
    cout << ret << '\n';
}
0