結果

問題 No.1098 LCAs
ユーザー HaaHaa
提出日時 2020-06-26 21:34:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 526 ms / 2,000 ms
コード長 744 bytes
コンパイル時間 1,538 ms
コンパイル使用メモリ 171,348 KB
実行使用メモリ 39,088 KB
最終ジャッジ日時 2023-09-18 03:17:55
合計ジャッジ時間 10,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
9,720 KB
testcase_01 AC 5 ms
9,648 KB
testcase_02 AC 5 ms
9,712 KB
testcase_03 AC 6 ms
9,704 KB
testcase_04 AC 5 ms
9,644 KB
testcase_05 AC 5 ms
9,592 KB
testcase_06 AC 5 ms
9,716 KB
testcase_07 AC 5 ms
9,664 KB
testcase_08 AC 6 ms
9,704 KB
testcase_09 AC 6 ms
9,728 KB
testcase_10 AC 5 ms
9,732 KB
testcase_11 AC 5 ms
9,560 KB
testcase_12 AC 5 ms
9,716 KB
testcase_13 AC 8 ms
9,716 KB
testcase_14 AC 8 ms
9,676 KB
testcase_15 AC 7 ms
9,644 KB
testcase_16 AC 7 ms
9,640 KB
testcase_17 AC 7 ms
9,708 KB
testcase_18 AC 488 ms
17,472 KB
testcase_19 AC 482 ms
17,316 KB
testcase_20 AC 482 ms
17,188 KB
testcase_21 AC 485 ms
17,184 KB
testcase_22 AC 488 ms
17,096 KB
testcase_23 AC 401 ms
20,508 KB
testcase_24 AC 409 ms
20,576 KB
testcase_25 AC 393 ms
20,456 KB
testcase_26 AC 433 ms
20,600 KB
testcase_27 AC 412 ms
20,656 KB
testcase_28 AC 519 ms
37,364 KB
testcase_29 AC 515 ms
39,088 KB
testcase_30 AC 526 ms
37,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 4611686018427387903;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

VVI edge(214514, VI(0));
VI ans(214514,0);

ll dfs(int v, int p){
	ll sum=0;
	VI a(0);
	for (auto to:edge[v]) {
		if (to!=p){
			ll k=dfs(to,v);
			sum+=k;
			a.push_back(k);
		}
	}
	ans[v]+=sum*2;
	for(auto x:a)
		ans[v]+=(sum-x)*x;
	ans[v]++;
	return sum+1;
}

int main(){
	int n; cin >> n;
	int a, b;
	REP(i,n-1){
		cin >> a >> b;
		a--; b--;
		edge[a].push_back(b);
		edge[b].push_back(a);
	}
	dfs(0,-1);
	REP(i,n)
		cout << ans[i] << endl;
	return 0;
}
0