結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー ocvret_ocvret_
提出日時 2021-03-05 22:18:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,983 bytes
コンパイル時間 2,054 ms
コンパイル使用メモリ 171,784 KB
実行使用メモリ 48,128 KB
最終ジャッジ日時 2024-04-16 09:54:53
合計ジャッジ時間 4,864 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 141 ms
17,280 KB
testcase_04 AC 132 ms
17,408 KB
testcase_05 AC 136 ms
17,408 KB
testcase_06 AC 132 ms
17,396 KB
testcase_07 AC 137 ms
17,408 KB
testcase_08 AC 80 ms
12,800 KB
testcase_09 AC 35 ms
7,808 KB
testcase_10 AC 38 ms
8,320 KB
testcase_11 AC 20 ms
6,144 KB
testcase_12 AC 64 ms
10,880 KB
testcase_13 AC 76 ms
12,160 KB
testcase_14 AC 78 ms
12,672 KB
testcase_15 AC 56 ms
10,240 KB
testcase_16 AC 7 ms
5,376 KB
testcase_17 AC 8 ms
5,376 KB
testcase_18 AC 129 ms
16,128 KB
testcase_19 AC 19 ms
5,632 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 59 ms
10,112 KB
testcase_22 AC 49 ms
9,088 KB
testcase_23 AC 5 ms
5,376 KB
testcase_24 AC 5 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 5 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 6 ms
5,376 KB
testcase_30 AC 6 ms
5,376 KB
testcase_31 AC 4 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 30 ms
13,312 KB
testcase_34 AC 160 ms
48,128 KB
testcase_35 AC 59 ms
21,888 KB
testcase_36 AC 12 ms
5,376 KB
testcase_37 AC 104 ms
19,764 KB
testcase_38 AC 97 ms
18,416 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 1 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>
// #include<atcoder/all>
// #include<boost/multiprecision/cpp_int.hpp>

using namespace std;
// using namespace atcoder;
// using bint = boost::multiprecision::cpp_int;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007


// 2020/12/31

struct Rerooting{
  struct DP{
    /*
      add variables ,edit constructer and functions
    */
    ll s = 0,x = 0;
    DP(){}
    DP operator+(const DP& o){
      DP res;
      res.x = this->x + o.x;
      res.s = this->s + o.s;
      return res;
    }
    DP addRoot()const{
      DP res(*this);
      res.x++;
      res.s += res.x;
      return res;
    }
  };
  vector<vector<int>> v;
  vector<vector<DP>> dp;
  vector<DP> res;
  Rerooting(int n = 0) : v(n),dp(n),res(n) {}
  void add_edge(int a,int b){
    v[a].push_back(b);
    v[b].push_back(a);
  }
  void init(){
    dfs(0);
    bfs(0);
  }
  DP dfs(int ov,int pre = -1){
    DP S;
    int deg = v[ov].size();
    dp[ov] = vector<DP>(deg);
    for(int i = 0;i < deg;i++){
      int nv = v[ov][i];
      if(nv == pre)continue;
      dp[ov][i] = dfs(nv,ov);
      S = S + dp[ov][i];
    }
    return S.addRoot();
  }
  void bfs(int ov,const DP &dp_pre = DP(),int pre = -1){
    int deg = v[ov].size();
    vector<DP> L(deg+1),R(deg+1);
    for(int i = 0;i < deg;i++)if(v[ov][i] == pre)dp[ov][i] = dp_pre;
    for(int i = 0;i < deg;i++)L[i+1] = L[i] + dp[ov][i];
    for(int i = deg-1;i >= 0;i--)R[i] = R[i+1] + dp[ov][i];
    res[ov] = L[deg].addRoot();
    for(int i = 0;i < deg;i++){
      int nv = v[ov][i];
      if(nv == pre)continue;
      bfs(nv,(L[i] + R[i+1]).addRoot(),ov);
    }
  }
};

int main(){
  
  int n;
  cin >> n;
  Rerooting rt(n);
  rep(i,n-1){
    int a,b;cin >> a >> b;
    a--;b--;
    rt.add_edge(a,b);
  }
  rt.init();
  ll res = 0;
  rep(i,n)res += rt.res[i].s;
  cout << res << "\n";
  

  return 0;
}
0