結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tada721tada721
提出日時 2021-02-05 19:04:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 985 bytes
コンパイル時間 828 ms
コンパイル使用メモリ 73,316 KB
実行使用メモリ 23,804 KB
最終ジャッジ日時 2023-09-15 04:10:33
合計ジャッジ時間 4,877 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,424 KB
testcase_01 AC 5 ms
9,480 KB
testcase_02 AC 5 ms
9,564 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 78 ms
13,812 KB
testcase_09 AC 35 ms
11,684 KB
testcase_10 AC 38 ms
11,672 KB
testcase_11 AC 21 ms
10,848 KB
testcase_12 AC 60 ms
12,888 KB
testcase_13 AC 74 ms
13,488 KB
testcase_14 AC 76 ms
13,828 KB
testcase_15 AC 54 ms
12,636 KB
testcase_16 AC 10 ms
9,864 KB
testcase_17 AC 10 ms
9,896 KB
testcase_18 AC 110 ms
15,468 KB
testcase_19 AC 19 ms
10,520 KB
testcase_20 AC 7 ms
9,600 KB
testcase_21 AC 52 ms
12,816 KB
testcase_22 AC 44 ms
12,108 KB
testcase_23 AC 8 ms
9,656 KB
testcase_24 AC 7 ms
9,660 KB
testcase_25 AC 7 ms
9,720 KB
testcase_26 AC 7 ms
9,700 KB
testcase_27 AC 5 ms
9,528 KB
testcase_28 AC 5 ms
9,472 KB
testcase_29 AC 8 ms
9,804 KB
testcase_30 AC 8 ms
9,808 KB
testcase_31 AC 6 ms
9,624 KB
testcase_32 AC 7 ms
9,612 KB
testcase_33 AC 25 ms
12,584 KB
testcase_34 WA -
testcase_35 AC 49 ms
15,308 KB
testcase_36 AC 13 ms
10,308 KB
testcase_37 AC 79 ms
15,784 KB
testcase_38 AC 72 ms
15,352 KB
testcase_39 AC 5 ms
9,704 KB
testcase_40 AC 5 ms
9,536 KB
testcase_41 AC 5 ms
9,624 KB
testcase_42 AC 4 ms
9,512 KB
testcase_43 AC 4 ms
9,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int depth[100100],subtreesize[100100];
int a[100100],b[100100];
vector<int> edge[100100],v[100100];
void dfs(int r,int p,int d){
	depth[r]=d;
	rep(i,edge[r].size())if(edge[r][i]!=p)dfs(edge[r][i],r,d+1);
	rep(i,edge[r].size())if(edge[r][i]!=p)subtreesize[r]+=subtreesize[edge[r][i]];	
	subtreesize[r]++;
}
int main(){
	int n;
	cin>>n;
	rep(i,n-1){
		cin>>a[i]>>b[i];
		a[i]--,b[i]--;
		edge[a[i]].push_back(b[i]);
		edge[b[i]].push_back(a[i]);
	}
	rep(i,n){
		if(edge[i].size()==1){
			dfs(i,-1,0);
			break;
		}
	}	
	rep(i,n-1){
		if(depth[a[i]]>depth[b[i]]){
			v[a[i]].push_back(n-subtreesize[a[i]]);
			v[b[i]].push_back(subtreesize[a[i]]);
		}
		else{
			v[b[i]].push_back(n-subtreesize[b[i]]);
			v[a[i]].push_back(subtreesize[b[i]]);
		}
	}
	long long ans=0;
	rep(i,n){
		ans+=2*n-1;
		rep(j,v[i].size()){
			ans+=v[i][j]*(n-1-v[i][j]);
		}
	}
	cout<<ans<<endl;
}
0