結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー m_tsubasam_tsubasa
提出日時 2021-03-05 21:49:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 704 bytes
コンパイル時間 2,582 ms
コンパイル使用メモリ 200,592 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2024-04-16 09:16:03
合計ジャッジ時間 4,945 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 96 ms
9,472 KB
testcase_04 AC 103 ms
9,472 KB
testcase_05 AC 101 ms
9,600 KB
testcase_06 AC 97 ms
9,472 KB
testcase_07 AC 98 ms
9,472 KB
testcase_08 AC 61 ms
7,424 KB
testcase_09 AC 28 ms
6,944 KB
testcase_10 AC 29 ms
6,944 KB
testcase_11 AC 17 ms
6,944 KB
testcase_12 AC 50 ms
6,944 KB
testcase_13 AC 59 ms
7,168 KB
testcase_14 AC 60 ms
7,424 KB
testcase_15 AC 44 ms
6,940 KB
testcase_16 AC 6 ms
6,940 KB
testcase_17 AC 7 ms
6,940 KB
testcase_18 AC 85 ms
8,960 KB
testcase_19 AC 15 ms
6,944 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 41 ms
6,940 KB
testcase_22 AC 36 ms
6,940 KB
testcase_23 AC 5 ms
6,944 KB
testcase_24 AC 5 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 6 ms
6,940 KB
testcase_30 AC 5 ms
6,944 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 4 ms
6,940 KB
testcase_33 AC 21 ms
6,940 KB
testcase_34 AC 100 ms
17,152 KB
testcase_35 AC 37 ms
9,088 KB
testcase_36 AC 9 ms
6,940 KB
testcase_37 AC 62 ms
9,440 KB
testcase_38 AC 58 ms
9,052 KB
testcase_39 AC 2 ms
6,940 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,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int n;
long long res = 0;
vector<vector<int>> g;
vector<long long> cnt;

long long solve();
long long dfs(int now = 0, int par = -1);

int main() {
  cin >> n;
  g.resize(n);
  for (int i = 1; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    g[--x].push_back(--y);
    g[y].push_back(x);
  }
  cout << solve() << endl;
  return 0;
}

long long solve() {
  cnt.assign(n, 1);
  dfs();
  return res + (long long)n * n;
}

long long dfs(int now, int par) {
  for (auto to : g[now])
    if (to != par) cnt[now] += dfs(to, now);
  res += cnt[now] * (n - cnt[now]);
  for (auto to : g[now])
    if (to != par) res += cnt[to] * (n - cnt[to]);
  return cnt[now];
}
0