結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー chocono2230chocono2230
提出日時 2021-03-05 22:17:51
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 1,579 bytes
コンパイル時間 2,170 ms
コンパイル使用メモリ 201,316 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2024-04-16 09:53:15
合計ジャッジ時間 4,895 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 105 ms
9,472 KB
testcase_04 AC 103 ms
9,472 KB
testcase_05 AC 98 ms
9,600 KB
testcase_06 AC 97 ms
9,600 KB
testcase_07 AC 99 ms
9,600 KB
testcase_08 AC 60 ms
7,424 KB
testcase_09 AC 27 ms
5,376 KB
testcase_10 AC 30 ms
5,504 KB
testcase_11 AC 17 ms
5,376 KB
testcase_12 AC 49 ms
6,656 KB
testcase_13 AC 61 ms
7,168 KB
testcase_14 AC 65 ms
7,552 KB
testcase_15 AC 45 ms
6,400 KB
testcase_16 AC 6 ms
5,376 KB
testcase_17 AC 6 ms
5,376 KB
testcase_18 AC 88 ms
8,960 KB
testcase_19 AC 15 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 44 ms
6,272 KB
testcase_22 AC 38 ms
5,888 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 5 ms
5,376 KB
testcase_30 AC 5 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 21 ms
7,424 KB
testcase_34 AC 107 ms
21,760 KB
testcase_35 AC 42 ms
11,136 KB
testcase_36 AC 8 ms
5,376 KB
testcase_37 AC 65 ms
9,444 KB
testcase_38 AC 59 ms
9,032 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 1 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

ll dpf(vector<vector<int>> &tree, vector<ll> &dp, int now, int bf) {
  ll res = 1;
  for(auto nx : tree.at(now)) {
    if(nx == bf) continue;
    res += dpf(tree, dp, nx, now);
  }
  return dp.at(now) = res;
}

void dfs(vector<vector<int>> &tree, vector<ll> &dp, int now, int bf, ll &ans, ll &sum) {
  for(auto nx : tree.at(now)) {
    if(nx == bf) continue;
    ll nxdp = dp.at(now);
    ll nowdp = dp.at(now) - dp.at(nx);
    ll pnxdp = dp.at(nx);
    ll pnowdp = dp.at(now);
    ll d = (nxdp + nowdp) - (dp.at(now) + dp.at(nx));
    sum += d;
    ans += sum;
    dp.at(now) = nowdp;
    dp.at(nx) = nxdp;
    dfs(tree, dp, nx, now, ans, sum);
    sum -= d;
    dp.at(now) = pnowdp;
    dp.at(nx) = pnxdp;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> tree(n, vector<int>());
  rep(i, n-1){
    int a, b;
    cin >> a >> b;
    a--; b--;
    tree.at(a).push_back(b);
    tree.at(b).push_back(a);
  }
  vector<ll> dp(n, 0);
  dpf(tree, dp, 0, -1);
  ll sum = accumulate(ALL(dp), 0LL);
  ll ans = sum;
  dfs(tree, dp, 0, -1, ans, sum);
  cout << ans << endl;
  return 0;
}
0