結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 hitonanodehitonanode
提出日時 2021-02-06 15:18:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,307 bytes
コンパイル時間 746 ms
コンパイル使用メモリ 80,048 KB
実行使用メモリ 15,544 KB
最終ジャッジ日時 2023-09-15 22:53:50
合計ジャッジ時間 4,225 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,996 KB
testcase_01 AC 3 ms
5,716 KB
testcase_02 AC 3 ms
5,756 KB
testcase_03 AC 92 ms
10,140 KB
testcase_04 AC 90 ms
10,184 KB
testcase_05 AC 90 ms
10,076 KB
testcase_06 AC 92 ms
10,080 KB
testcase_07 AC 90 ms
10,268 KB
testcase_08 AC 57 ms
8,580 KB
testcase_09 AC 28 ms
6,912 KB
testcase_10 AC 30 ms
6,964 KB
testcase_11 AC 18 ms
6,312 KB
testcase_12 AC 46 ms
8,076 KB
testcase_13 AC 55 ms
8,228 KB
testcase_14 AC 58 ms
8,644 KB
testcase_15 AC 43 ms
7,796 KB
testcase_16 AC 8 ms
5,952 KB
testcase_17 AC 8 ms
5,964 KB
testcase_18 AC 80 ms
9,660 KB
testcase_19 AC 16 ms
6,356 KB
testcase_20 AC 5 ms
5,836 KB
testcase_21 AC 42 ms
7,912 KB
testcase_22 AC 36 ms
7,328 KB
testcase_23 AC 6 ms
5,980 KB
testcase_24 AC 6 ms
5,844 KB
testcase_25 AC 5 ms
5,840 KB
testcase_26 AC 5 ms
5,832 KB
testcase_27 AC 4 ms
5,840 KB
testcase_28 AC 4 ms
5,720 KB
testcase_29 AC 7 ms
6,120 KB
testcase_30 AC 6 ms
5,920 KB
testcase_31 AC 5 ms
5,800 KB
testcase_32 AC 5 ms
5,860 KB
testcase_33 AC 19 ms
7,632 KB
testcase_34 AC 85 ms
15,544 KB
testcase_35 AC 36 ms
9,756 KB
testcase_36 AC 10 ms
6,356 KB
testcase_37 AC 58 ms
10,276 KB
testcase_38 AC 51 ms
9,616 KB
testcase_39 AC 3 ms
5,660 KB
testcase_40 AC 3 ms
5,816 KB
testcase_41 AC 3 ms
5,708 KB
testcase_42 AC 3 ms
5,664 KB
testcase_43 AC 3 ms
5,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<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_back(b);
        to[b].push_back(a);
    }

    vector<long long> dpx(N), dpxy(N);

    auto dfs1 = [&](auto self, int now, int prv) -> void {
        for (auto nxt : to[now]) {
            if (nxt != prv) {
                self(self, nxt, now);
                dpx[now] += dpx[nxt];
                dpxy[now] += dpxy[nxt];
            }
        }
        dpx[now] += 1;
        dpxy[now] += dpx[now];
    };
    dfs1(dfs1, 0, -1);

    long long ret = 0;
    auto dfs2 = [&](auto self, int now, int prv, long long dpxpar, long long dpxypar) -> void {
        ret += dpxy[now] + dpxypar + dpxpar;
        for (auto nxt : to[now]) {
            if (nxt != prv) {
                dpxpar += dpx[nxt];
                dpxypar += dpxy[nxt];
            }
        }

        for (auto nxt : to[now]) {
            if (nxt != prv) {
                long long a = dpxpar - dpx[nxt] + 1;
                long long b = dpxypar - dpxy[nxt] + a;
                self(self, nxt, now, a, b);
            }
        }
    };
    dfs2(dfs2, 0, -1, 0, 0);

    cout << ret << '\n';
}
0