結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー tada721tada721
提出日時 2021-02-05 19:07:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 991 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 74,248 KB
実行使用メモリ 25,116 KB
最終ジャッジ日時 2023-09-15 19:47:19
合計ジャッジ時間 4,607 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,536 KB
testcase_01 AC 4 ms
9,500 KB
testcase_02 AC 4 ms
9,792 KB
testcase_03 AC 116 ms
18,720 KB
testcase_04 AC 113 ms
18,492 KB
testcase_05 AC 107 ms
18,620 KB
testcase_06 AC 108 ms
18,464 KB
testcase_07 AC 108 ms
18,568 KB
testcase_08 AC 71 ms
15,792 KB
testcase_09 AC 34 ms
12,328 KB
testcase_10 AC 37 ms
12,576 KB
testcase_11 AC 21 ms
11,200 KB
testcase_12 AC 56 ms
14,260 KB
testcase_13 AC 67 ms
15,112 KB
testcase_14 AC 71 ms
15,564 KB
testcase_15 AC 52 ms
13,864 KB
testcase_16 AC 9 ms
10,076 KB
testcase_17 AC 10 ms
10,164 KB
testcase_18 AC 102 ms
17,956 KB
testcase_19 AC 19 ms
11,024 KB
testcase_20 AC 6 ms
9,748 KB
testcase_21 AC 50 ms
13,732 KB
testcase_22 AC 44 ms
13,188 KB
testcase_23 AC 7 ms
9,964 KB
testcase_24 AC 7 ms
9,824 KB
testcase_25 AC 6 ms
9,756 KB
testcase_26 AC 7 ms
9,796 KB
testcase_27 AC 4 ms
9,552 KB
testcase_28 AC 5 ms
9,876 KB
testcase_29 AC 8 ms
9,928 KB
testcase_30 AC 8 ms
9,884 KB
testcase_31 AC 6 ms
9,820 KB
testcase_32 AC 6 ms
9,820 KB
testcase_33 AC 25 ms
12,968 KB
testcase_34 AC 109 ms
25,116 KB
testcase_35 AC 44 ms
16,024 KB
testcase_36 AC 12 ms
10,464 KB
testcase_37 AC 72 ms
17,988 KB
testcase_38 AC 67 ms
17,188 KB
testcase_39 AC 5 ms
9,584 KB
testcase_40 AC 4 ms
9,484 KB
testcase_41 AC 4 ms
9,568 KB
testcase_42 AC 4 ms
9,488 KB
testcase_43 AC 4 ms
9,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<algorithm>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
using namespace std;
ll depth[100100],subtreesize[100100];
ll a[100100],b[100100];
vector<ll> edge[100100],v[100100];
void dfs(ll r,ll p,ll 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(){
	ll 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]]);
		}
	}
	ll 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