結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー 沙耶花沙耶花
提出日時 2021-03-05 21:44:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 2,000 ms
コード長 721 bytes
コンパイル時間 4,287 ms
コンパイル使用メモリ 261,616 KB
実行使用メモリ 18,768 KB
最終ジャッジ日時 2023-07-29 01:24:54
合計ジャッジ時間 6,929 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 63 ms
9,500 KB
testcase_04 AC 64 ms
9,580 KB
testcase_05 AC 61 ms
9,472 KB
testcase_06 AC 65 ms
9,480 KB
testcase_07 AC 64 ms
9,408 KB
testcase_08 AC 36 ms
7,364 KB
testcase_09 AC 15 ms
5,220 KB
testcase_10 AC 17 ms
5,288 KB
testcase_11 AC 9 ms
4,456 KB
testcase_12 AC 26 ms
6,488 KB
testcase_13 AC 32 ms
7,092 KB
testcase_14 AC 34 ms
7,292 KB
testcase_15 AC 25 ms
6,300 KB
testcase_16 AC 4 ms
4,380 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 56 ms
8,992 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 25 ms
6,408 KB
testcase_22 AC 21 ms
5,896 KB
testcase_23 AC 3 ms
4,384 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 12 ms
6,560 KB
testcase_34 AC 60 ms
18,768 KB
testcase_35 AC 23 ms
9,792 KB
testcase_36 AC 5 ms
4,384 KB
testcase_37 AC 36 ms
9,420 KB
testcase_38 AC 32 ms
8,912 KB
testcase_39 AC 1 ms
4,376 KB
testcase_40 AC 1 ms
4,380 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint1000000007;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001
long long ans = 0LL;
int n;
vector<long long> sz;
vector<vector<int>> E;

void dfs(int cur,int p){
	sz[cur] = 1;
	ans += n;
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dfs(to,cur);
		ans += sz[to] * (n-sz[to]);
		sz[cur] += sz[to];
	}
	ans += (n-sz[cur]) * sz[cur];
}

int main(){
	
	
	cin>>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);
	}
	sz.resize(n,0);
	
	dfs(0,-1);
	
	cout<<ans<<endl;
	
    return 0;
}
0