結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 hitonanodehitonanode
提出日時 2021-02-06 11:42:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 849 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 72,644 KB
実行使用メモリ 13,204 KB
最終ジャッジ日時 2023-09-15 15:59:36
合計ジャッジ時間 4,824 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 83 ms
10,460 KB
testcase_04 AC 86 ms
10,460 KB
testcase_05 AC 87 ms
10,356 KB
testcase_06 AC 84 ms
10,356 KB
testcase_07 AC 87 ms
10,416 KB
testcase_08 AC 56 ms
8,432 KB
testcase_09 AC 25 ms
6,192 KB
testcase_10 AC 29 ms
6,272 KB
testcase_11 AC 16 ms
5,328 KB
testcase_12 AC 44 ms
7,464 KB
testcase_13 AC 52 ms
8,036 KB
testcase_14 AC 53 ms
8,356 KB
testcase_15 AC 40 ms
7,180 KB
testcase_16 AC 7 ms
4,476 KB
testcase_17 AC 6 ms
4,500 KB
testcase_18 AC 74 ms
9,804 KB
testcase_19 AC 14 ms
5,212 KB
testcase_20 AC 4 ms
4,380 KB
testcase_21 AC 38 ms
7,260 KB
testcase_22 AC 35 ms
6,728 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 4 ms
4,448 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 6 ms
4,396 KB
testcase_30 AC 5 ms
4,408 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 4 ms
4,376 KB
testcase_33 AC 20 ms
6,296 KB
testcase_34 AC 85 ms
13,204 KB
testcase_35 AC 33 ms
7,904 KB
testcase_36 AC 9 ms
5,188 KB
testcase_37 AC 59 ms
9,968 KB
testcase_38 AC 55 ms
9,572 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 3 ms
4,380 KB
testcase_43 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <forward_list>
#include <iostream>
using namespace std;

forward_list<int> to[100000];

int main() {

    int N;
    cin >> N;

    for (int i = 0; i < N - 1; i++) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        to[a].push_front(b);
        to[b].push_front(a);
    }

    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