結果

問題 No.1418 Sum of Sum of Subtree Size
ユーザー y61mpnly61mpnl
提出日時 2021-03-06 00:38:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 832 bytes
コンパイル時間 2,034 ms
コンパイル使用メモリ 196,700 KB
実行使用メモリ 19,036 KB
最終ジャッジ日時 2024-04-16 12:35:26
合計ジャッジ時間 4,166 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,016 KB
testcase_01 AC 4 ms
6,272 KB
testcase_02 AC 4 ms
6,144 KB
testcase_03 AC 57 ms
11,456 KB
testcase_04 AC 53 ms
11,584 KB
testcase_05 AC 54 ms
11,508 KB
testcase_06 AC 51 ms
11,528 KB
testcase_07 AC 54 ms
11,628 KB
testcase_08 AC 33 ms
9,924 KB
testcase_09 AC 17 ms
7,936 KB
testcase_10 AC 18 ms
8,192 KB
testcase_11 AC 11 ms
7,168 KB
testcase_12 AC 25 ms
9,216 KB
testcase_13 AC 32 ms
9,576 KB
testcase_14 AC 33 ms
9,872 KB
testcase_15 AC 27 ms
9,088 KB
testcase_16 AC 6 ms
6,528 KB
testcase_17 AC 5 ms
6,656 KB
testcase_18 AC 47 ms
10,928 KB
testcase_19 AC 10 ms
7,168 KB
testcase_20 AC 4 ms
6,144 KB
testcase_21 AC 25 ms
8,916 KB
testcase_22 AC 22 ms
8,448 KB
testcase_23 AC 5 ms
6,272 KB
testcase_24 AC 4 ms
6,400 KB
testcase_25 AC 4 ms
6,272 KB
testcase_26 AC 5 ms
6,400 KB
testcase_27 AC 3 ms
6,144 KB
testcase_28 AC 4 ms
6,016 KB
testcase_29 AC 5 ms
6,400 KB
testcase_30 AC 6 ms
6,400 KB
testcase_31 AC 4 ms
6,400 KB
testcase_32 AC 4 ms
6,400 KB
testcase_33 AC 13 ms
9,344 KB
testcase_34 AC 60 ms
19,036 KB
testcase_35 AC 24 ms
11,776 KB
testcase_36 AC 7 ms
6,912 KB
testcase_37 AC 35 ms
11,404 KB
testcase_38 AC 30 ms
10,928 KB
testcase_39 AC 3 ms
6,144 KB
testcase_40 AC 3 ms
6,016 KB
testcase_41 AC 3 ms
6,272 KB
testcase_42 AC 3 ms
6,016 KB
testcase_43 AC 3 ms
6,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define REP(i,b,e) for(int i=b;i<e;i++)
using ll = int_fast64_t;

std::vector<int> links[100000], visit;
int in[100000], out[100000], from[100000];
ll cnt[100001];
bool used[100000];

void eular_tour(int v=0){
	if(used[v]) return;
	used[v] = true;
	in[v] = visit.size();
	visit.push_back(v);
	for(auto to: links[v]){
		if(!used[to]){
			from[to] = v;
			eular_tour(to);
		}
	}
	out[v] = visit.size();
}

int main(){
	ll n;
	scanf("%ld", &n);
	REP(i, 0, n-1){
		int a, b;
		scanf("%d %d", &a, &b);
		links[a-1].push_back(b-1);
		links[b-1].push_back(a-1);
	}

	eular_tour();
	REP(i, 0, n) cnt[i] = out[i] - in[i];

	ll ans = 0;
	REP(i, 0, n){
		ans += n;
		for(auto x: links[i]){
			if(from[i]!=x) ans += cnt[x] * (n - cnt[x]);
			else ans += cnt[i] * (n - cnt[i]);
		}
	}
	printf("%ld\n", ans);
	return 0;
}
0