結果

問題 No.1098 LCAs
ユーザー snrnsidysnrnsidy
提出日時 2021-05-13 04:00:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 232 ms / 2,000 ms
コード長 826 bytes
コンパイル時間 2,374 ms
コンパイル使用メモリ 203,384 KB
実行使用メモリ 29,340 KB
最終ジャッジ日時 2023-10-24 21:57:40
合計ジャッジ時間 8,705 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,780 KB
testcase_01 AC 4 ms
9,780 KB
testcase_02 AC 4 ms
9,720 KB
testcase_03 AC 4 ms
9,780 KB
testcase_04 AC 4 ms
9,780 KB
testcase_05 AC 4 ms
9,780 KB
testcase_06 AC 4 ms
9,780 KB
testcase_07 AC 4 ms
9,780 KB
testcase_08 AC 4 ms
9,780 KB
testcase_09 AC 3 ms
9,780 KB
testcase_10 AC 4 ms
9,784 KB
testcase_11 AC 4 ms
9,784 KB
testcase_12 AC 4 ms
9,784 KB
testcase_13 AC 4 ms
9,832 KB
testcase_14 AC 5 ms
9,832 KB
testcase_15 AC 4 ms
9,832 KB
testcase_16 AC 4 ms
9,832 KB
testcase_17 AC 4 ms
9,832 KB
testcase_18 AC 167 ms
17,928 KB
testcase_19 AC 170 ms
17,928 KB
testcase_20 AC 166 ms
17,928 KB
testcase_21 AC 172 ms
17,928 KB
testcase_22 AC 169 ms
17,924 KB
testcase_23 AC 101 ms
18,116 KB
testcase_24 AC 104 ms
18,120 KB
testcase_25 AC 94 ms
18,076 KB
testcase_26 AC 104 ms
18,076 KB
testcase_27 AC 102 ms
18,076 KB
testcase_28 AC 232 ms
28,400 KB
testcase_29 AC 231 ms
29,340 KB
testcase_30 AC 229 ms
28,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

long long int dp[200005];
vector <int> adj[200005];
long long int cnt[200005];

long long int func(int now, int prev)
{
	cnt[now] = 1;
	for (auto next : adj[now])
	{
		if (next == prev) continue;
		cnt[now] += func(next, now);
	}
	return cnt[now];
}

void solve(int now, int prev)
{
	//cout << now << ' ' << cnt[now] << '\n';
	dp[now] = cnt[now] * cnt[now];
	for (auto next : adj[now])
	{
		if (next == prev) continue;
		solve(next, now);
		dp[now] -= (cnt[next] * cnt[next]);
	}
}

int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int n, a, b;

	cin >> n;

	for (int i = 0; i < n - 1; i++)
	{
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	
	func(1, 0);
	solve(1, 0);

	for (int i = 1; i <= n; i++)
	{
		cout << dp[i] << '\n';
	}

	return 0;
}
0