結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー trineutrontrineutron
提出日時 2021-03-05 23:12:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 119 ms / 2,000 ms
コード長 1,057 bytes
コンパイル時間 2,302 ms
コンパイル使用メモリ 199,788 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-16 11:10:54
合計ジャッジ時間 5,484 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 117 ms
10,240 KB
testcase_04 AC 119 ms
10,340 KB
testcase_05 AC 116 ms
10,368 KB
testcase_06 AC 116 ms
10,368 KB
testcase_07 AC 119 ms
10,368 KB
testcase_08 AC 74 ms
7,936 KB
testcase_09 AC 33 ms
6,940 KB
testcase_10 AC 36 ms
6,944 KB
testcase_11 AC 18 ms
6,940 KB
testcase_12 AC 59 ms
7,168 KB
testcase_13 AC 69 ms
7,680 KB
testcase_14 AC 72 ms
7,936 KB
testcase_15 AC 53 ms
6,944 KB
testcase_16 AC 7 ms
6,940 KB
testcase_17 AC 7 ms
6,940 KB
testcase_18 AC 106 ms
9,728 KB
testcase_19 AC 16 ms
6,940 KB
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 52 ms
6,940 KB
testcase_22 AC 42 ms
6,940 KB
testcase_23 AC 4 ms
6,944 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 5 ms
6,940 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 5 ms
6,940 KB
testcase_30 AC 6 ms
6,940 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 4 ms
6,944 KB
testcase_33 AC 22 ms
6,940 KB
testcase_34 AC 115 ms
19,456 KB
testcase_35 AC 45 ms
9,984 KB
testcase_36 AC 10 ms
6,940 KB
testcase_37 AC 68 ms
10,212 KB
testcase_38 AC 63 ms
9,656 KB
testcase_39 AC 2 ms
6,948 KB
testcase_40 AC 2 ms
6,940 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 #

#include <bits/stdc++.h>
using namespace std;

using graph = vector<vector<int>>;

void dfs(const graph &to, vector<int64_t> &child, vector<int64_t> &par, int v, int p)
{
    par.at(v) = p;
    for (auto &&i : to.at(v))
    {
        if (i == p)
            continue;
        dfs(to, child, par, i, v);
        child.at(v) += child.at(i);
    }
    child.at(v)++;
}

int main(int argc, char const *argv[])
{
    int64_t n;
    cin >> n;
    graph to(n);
    for (int i = 0; i < n - 1; i++)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        to.at(a).push_back(b);
        to.at(b).push_back(a);
    }
    vector<int64_t> child(n), par(n);
    dfs(to, child, par, 0, -1);
    assert(child.at(0) == n);
    int64_t ans = n * n * n;
    for (int i = 0; i < n; i++)
    {
        for (auto &&c : to.at(i))
        {
            if (c == par.at(i))
                continue;
            ans -= child.at(c) * child.at(c);
        }
        ans -= (n - child.at(i)) * (n - child.at(i));
    }
    cout << ans << endl;
    return 0;
}
0