結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー SSRSSSRS
提出日時 2021-03-05 21:40:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 944 bytes
コンパイル時間 1,889 ms
コンパイル使用メモリ 173,972 KB
実行使用メモリ 16,528 KB
最終ジャッジ日時 2024-04-16 09:05:14
合計ジャッジ時間 4,916 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 118 ms
15,332 KB
testcase_04 AC 121 ms
15,328 KB
testcase_05 AC 116 ms
15,312 KB
testcase_06 AC 118 ms
15,580 KB
testcase_07 AC 114 ms
15,328 KB
testcase_08 AC 75 ms
11,284 KB
testcase_09 AC 31 ms
7,040 KB
testcase_10 AC 35 ms
7,424 KB
testcase_11 AC 19 ms
6,940 KB
testcase_12 AC 57 ms
9,728 KB
testcase_13 AC 70 ms
10,752 KB
testcase_14 AC 71 ms
11,188 KB
testcase_15 AC 52 ms
9,216 KB
testcase_16 AC 7 ms
6,940 KB
testcase_17 AC 7 ms
6,944 KB
testcase_18 AC 105 ms
14,340 KB
testcase_19 AC 17 ms
6,944 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 49 ms
8,960 KB
testcase_22 AC 43 ms
8,448 KB
testcase_23 AC 5 ms
6,940 KB
testcase_24 AC 4 ms
6,944 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 5 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 6 ms
6,944 KB
testcase_30 AC 5 ms
6,940 KB
testcase_31 AC 3 ms
6,944 KB
testcase_32 AC 4 ms
6,944 KB
testcase_33 AC 21 ms
6,940 KB
testcase_34 AC 110 ms
16,528 KB
testcase_35 AC 39 ms
8,704 KB
testcase_36 AC 10 ms
6,940 KB
testcase_37 AC 70 ms
14,272 KB
testcase_38 AC 63 ms
13,196 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,940 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    E[A].push_back(B);
    E[B].push_back(A);
  }
  vector<int> p(N, -1);
  vector<vector<int>> c(N);
  vector<int> b;
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()){
    int v = Q.front();
    Q.pop();
    b.push_back(v);
    for (int w : E[v]){
      if (w != p[v]){
        p[w] = v;
        c[v].push_back(w);
        Q.push(w);
      }
    }
  }
  reverse(b.begin(), b.end());
  vector<long long> dp1(N, 1), dp2(N, 0);
  long long ans = 0;
  for (int v : b){
    int d = c[v].size();
    for (int w : c[v]){
      dp1[v] += dp1[w];
    }
    for (int w : c[v]){
      ans += (dp2[w] + dp1[w]) * (dp1[v] - dp1[w]) * 2;
    }
    for (int w : c[v]){
      dp2[v] += dp2[w] + dp1[w];
    }
  }
  ans += (long long) N * N;
  cout << ans << endl;
}
0