結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー t98slidert98slider
提出日時 2021-07-29 03:20:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 564 bytes
コンパイル時間 1,572 ms
コンパイル使用メモリ 170,724 KB
実行使用メモリ 16,180 KB
最終ジャッジ日時 2023-10-12 01:57:36
合計ジャッジ時間 5,363 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 92 ms
8,588 KB
testcase_04 AC 95 ms
8,744 KB
testcase_05 AC 93 ms
8,588 KB
testcase_06 AC 93 ms
8,432 KB
testcase_07 AC 91 ms
8,596 KB
testcase_08 AC 56 ms
6,792 KB
testcase_09 AC 25 ms
4,992 KB
testcase_10 AC 28 ms
4,900 KB
testcase_11 AC 15 ms
4,352 KB
testcase_12 AC 45 ms
5,980 KB
testcase_13 AC 54 ms
6,524 KB
testcase_14 AC 56 ms
6,740 KB
testcase_15 AC 41 ms
5,608 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 6 ms
4,348 KB
testcase_18 AC 80 ms
7,948 KB
testcase_19 AC 13 ms
4,352 KB
testcase_20 AC 3 ms
4,352 KB
testcase_21 AC 39 ms
5,600 KB
testcase_22 AC 33 ms
5,412 KB
testcase_23 AC 4 ms
4,352 KB
testcase_24 AC 4 ms
4,348 KB
testcase_25 AC 3 ms
4,352 KB
testcase_26 AC 4 ms
4,348 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 4 ms
4,352 KB
testcase_30 AC 4 ms
4,352 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 4 ms
4,348 KB
testcase_33 AC 19 ms
5,804 KB
testcase_34 AC 98 ms
16,180 KB
testcase_35 AC 37 ms
8,480 KB
testcase_36 AC 8 ms
4,352 KB
testcase_37 AC 58 ms
8,400 KB
testcase_38 AC 54 ms
8,072 KB
testcase_39 AC 2 ms
4,348 KB
testcase_40 AC 1 ms
4,348 KB
testcase_41 AC 1 ms
4,348 KB
testcase_42 AC 1 ms
4,348 KB
testcase_43 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

int main(){
    long n,u,v,ans=0;
    cin>>n;
    vector<vector<int>> g(n);
    for(int i=0;i<n-1;i++){
        cin>>u>>v;
        g[--u].push_back(--v);
        g[v].push_back(u);
    }
    function<int(int,int)> dfs=[&](int u,int p){
        long ret=1,d;
        ans+=n;
        for(auto v:g[u]){
            if(v!=p){
                d=dfs(v,u);
                ret+=d;
                ans+=d*(n-d);
            }
        }
        ans+=ret*(n-ret);
        return ret;
    };
    dfs(0,-1);
    cout<<ans<<endl;
}
0