結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 👑 NachiaNachia
提出日時 2021-03-05 22:15:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 911 bytes
コンパイル時間 5,126 ms
コンパイル使用メモリ 258,664 KB
実行使用メモリ 10,816 KB
最終ジャッジ日時 2024-04-16 09:50:59
合計ジャッジ時間 6,778 ms
ジャッジサーバーID
(参考情報)
judge3 / 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 61 ms
10,588 KB
testcase_04 AC 61 ms
10,596 KB
testcase_05 AC 56 ms
10,596 KB
testcase_06 AC 61 ms
10,720 KB
testcase_07 AC 62 ms
10,720 KB
testcase_08 AC 38 ms
8,288 KB
testcase_09 AC 17 ms
5,760 KB
testcase_10 AC 18 ms
6,016 KB
testcase_11 AC 11 ms
5,376 KB
testcase_12 AC 28 ms
7,296 KB
testcase_13 AC 33 ms
8,064 KB
testcase_14 AC 36 ms
8,212 KB
testcase_15 AC 26 ms
7,040 KB
testcase_16 AC 4 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 55 ms
9,960 KB
testcase_19 AC 10 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 25 ms
7,040 KB
testcase_22 AC 22 ms
6,400 KB
testcase_23 AC 4 ms
5,376 KB
testcase_24 AC 4 ms
5,376 KB
testcase_25 AC 3 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 4 ms
5,376 KB
testcase_30 AC 4 ms
5,376 KB
testcase_31 AC 3 ms
5,376 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 11 ms
5,376 KB
testcase_34 AC 57 ms
10,492 KB
testcase_35 AC 21 ms
6,400 KB
testcase_36 AC 6 ms
5,376 KB
testcase_37 AC 38 ms
10,816 KB
testcase_38 AC 32 ms
10,204 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

using mll=atcoder::static_modint<1000000007>;

int N;
vector<vector<int>> E;
vector<int> gI,gP;
vector<ll> Z;

int main(){
  scanf("%d",&N);
  E.resize(N);
  rep(i,N-1){
    int u,v; scanf("%d%d",&u,&v); u--; v--;
    E[u].push_back(v);
    E[v].push_back(u);
  }
  gP.assign(N,-1);
  {
    queue<int> Q; Q.push(0);
    while(Q.size()){
      int p=Q.front(); Q.pop();
      gI.push_back(p);
      for(int e:E[p]) if(e!=gP[p]){
        gP[e]=p;
        Q.push(e);
      }
    }
  }
  Z.assign(N,1);
  for(int i=N-1; i>=0; i--){
    int p=gI[i];
    for(int e:E[p]) if(gP[p]!=e) Z[p]+=Z[e];
  }
  ll ans=0;
  rep(p,N){
    ans+=N;
    ans+=Z[p]*(N-Z[p]);
    for(int e:E[p]) if(gP[p]!=e) ans+=(N-Z[e])*Z[e];
  }
  printf("%lld\n",ans);
  return 0;
}
0