結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 100 ms
10,660 KB
testcase_04 AC 105 ms
10,752 KB
testcase_05 AC 108 ms
10,752 KB
testcase_06 AC 107 ms
10,752 KB
testcase_07 AC 106 ms
10,880 KB
testcase_08 AC 62 ms
8,448 KB
testcase_09 AC 28 ms
6,944 KB
testcase_10 AC 31 ms
6,940 KB
testcase_11 AC 17 ms
6,940 KB
testcase_12 AC 51 ms
7,168 KB
testcase_13 AC 62 ms
8,064 KB
testcase_14 AC 64 ms
8,064 KB
testcase_15 AC 45 ms
6,944 KB
testcase_16 AC 6 ms
6,940 KB
testcase_17 AC 6 ms
6,944 KB
testcase_18 AC 91 ms
10,112 KB
testcase_19 AC 15 ms
6,940 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 44 ms
6,944 KB
testcase_22 AC 37 ms
6,940 KB
testcase_23 AC 4 ms
6,944 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 5 ms
6,944 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 20 ms
6,940 KB
testcase_34 AC 105 ms
18,432 KB
testcase_35 AC 39 ms
9,600 KB
testcase_36 AC 9 ms
6,940 KB
testcase_37 AC 65 ms
10,604 KB
testcase_38 AC 61 ms
9,916 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
testcase_41 AC 2 ms
6,944 KB
testcase_42 AC 2 ms
6,940 KB
testcase_43 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//g++ 5.cpp -std=c++14 -I .
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
#define fi first
#define se second
#define pb push_back

int x[100500];
int dist[100500];
int seen[100500]={};

void dfs(vector<vector<int>> &g,int now){
  seen[now]=1;
  int z=0;
  for(int next:g[now]){
    if(seen[next]==0){
      dist[next]=dist[now]+1;
      dfs(g,next);
      z+=x[next];
    }
  }
  x[now]=z+1;
}

int main(){
  int n;
  cin>>n;
  vector<vector<int>> g(n);
  int v[n-1][2];
  for(int i=0;i<n-1;i++){
    int a,b;
    cin>>a>>b;
    a--;
    b--;
    g[a].pb(b);
    g[b].pb(a);
    v[i][0]=a,v[i][1]=b;
  }
  dist[0]=0;
  dfs(g,0);
  ll ans=0;
  
  for(int i=0;i<n-1;i++){
    int a=v[i][0],b=v[i][1];
    if(dist[a]>dist[b]){
      swap(a,b);
    }
    ans+=((ll)x[b])*(n-x[b]);
  }
  ans*=2;
  ans+=(ll)n*n;
  cout<<ans<<endl;
}
0