結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー Ricky_ponRicky_pon
提出日時 2021-03-05 22:30:11
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 75 ms / 2,000 ms
コード長 1,864 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 195,792 KB
実行使用メモリ 18,304 KB
最終ジャッジ日時 2024-04-16 10:14:53
合計ジャッジ時間 4,714 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,272 KB
testcase_01 AC 3 ms
5,888 KB
testcase_02 AC 4 ms
6,016 KB
testcase_03 AC 71 ms
10,660 KB
testcase_04 AC 66 ms
10,752 KB
testcase_05 AC 70 ms
10,824 KB
testcase_06 AC 65 ms
10,752 KB
testcase_07 AC 75 ms
10,820 KB
testcase_08 AC 42 ms
9,216 KB
testcase_09 AC 19 ms
7,552 KB
testcase_10 AC 21 ms
7,808 KB
testcase_11 AC 12 ms
6,912 KB
testcase_12 AC 31 ms
8,448 KB
testcase_13 AC 38 ms
8,960 KB
testcase_14 AC 39 ms
9,280 KB
testcase_15 AC 28 ms
8,576 KB
testcase_16 AC 6 ms
6,528 KB
testcase_17 AC 6 ms
6,400 KB
testcase_18 AC 60 ms
10,496 KB
testcase_19 AC 10 ms
6,784 KB
testcase_20 AC 5 ms
6,400 KB
testcase_21 AC 28 ms
8,448 KB
testcase_22 AC 24 ms
7,936 KB
testcase_23 AC 5 ms
6,400 KB
testcase_24 AC 5 ms
6,144 KB
testcase_25 AC 4 ms
6,272 KB
testcase_26 AC 5 ms
6,144 KB
testcase_27 AC 3 ms
6,144 KB
testcase_28 AC 3 ms
6,272 KB
testcase_29 AC 5 ms
6,400 KB
testcase_30 AC 5 ms
6,400 KB
testcase_31 AC 4 ms
6,272 KB
testcase_32 AC 5 ms
6,272 KB
testcase_33 AC 14 ms
8,704 KB
testcase_34 AC 72 ms
18,304 KB
testcase_35 AC 27 ms
11,264 KB
testcase_36 AC 7 ms
6,528 KB
testcase_37 AC 39 ms
10,488 KB
testcase_38 AC 36 ms
10,104 KB
testcase_39 AC 3 ms
6,272 KB
testcase_40 AC 3 ms
6,144 KB
testcase_41 AC 3 ms
6,016 KB
testcase_42 AC 3 ms
6,144 KB
testcase_43 AC 4 ms
6,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (int)(a); (i) < (int)(b); ++(i))
#define rFor(i, a, b) for (int(i) = (int)(a)-1; (i) >= (int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
T div_floor(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a >= 0 ? a / b : (a + 1) / b - 1;
}
template <class T>
T div_ceil(T a, T b) {
    if (b < 0) a *= -1, b *= -1;
    return a > 0 ? (a - 1) / b + 1 : a / b;
}

constexpr lint mod = 1000000007;
constexpr lint INF = mod * mod;
constexpr int MAX = 100010;

int n;
vector<int> G[MAX];
lint ans[MAX], sz[MAX];

void sz_dfs(int v, int pv) {
    sz[v] = 1;
    for (auto nv : G[v])
        if (nv != pv) {
            sz_dfs(nv, v);
            sz[v] += sz[nv];
            ans[v] += ans[nv];
        }
    ans[v] += sz[v];
}

void reroot_dfs(int v, int pv, lint par) {
    ans[v] += par;
    lint sum = 0;
    for (auto nv : G[v])
        if (nv != pv) sum += ans[nv];
    for (auto nv : G[v])
        if (nv != pv) {
            reroot_dfs(nv, v, par + sum - ans[nv] + n - sz[nv]);
        }
}

int main() {
    scanf("%d", &n);
    rep(i, n - 1) {
        int a, b;
        scanf("%d%d", &a, &b);
        --a;
        --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    sz_dfs(0, -1);
    reroot_dfs(0, -1, 0);
    lint sum = 0;
    rep(i, n) sum += ans[i] + n - sz[i];
    printf("%lld\n", sum);
}
0