結果

問題 No.1098 LCAs
ユーザー nanophoto12nanophoto12
提出日時 2020-06-27 13:45:49
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 555 ms / 2,000 ms
コード長 1,238 bytes
コンパイル時間 3,156 ms
コンパイル使用メモリ 203,936 KB
実行使用メモリ 40,184 KB
最終ジャッジ日時 2023-09-18 19:22:31
合計ジャッジ時間 10,511 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 4 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 501 ms
22,848 KB
testcase_19 AC 507 ms
23,020 KB
testcase_20 AC 509 ms
22,996 KB
testcase_21 AC 499 ms
23,000 KB
testcase_22 AC 501 ms
22,892 KB
testcase_23 AC 411 ms
21,940 KB
testcase_24 AC 413 ms
21,944 KB
testcase_25 AC 397 ms
21,824 KB
testcase_26 AC 432 ms
22,160 KB
testcase_27 AC 417 ms
21,872 KB
testcase_28 AC 551 ms
39,132 KB
testcase_29 AC 555 ms
40,184 KB
testcase_30 AC 553 ms
38,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

const ll mod = 1000000007;

vector<vector<int>> g;
vector<int> dp;
vector<vector<int>> children;

int dfs(int current, int parent) {
	if (dp[current]) {
		return dp[current];
	}
	int sum = 1;
	for (int i = 0; i < g[current].size(); i++) {
		if (g[current][i] != parent) {
			auto d = dfs(g[current][i], current);
			sum += d;
			children[current].push_back(d);
		}
	}
	dp[current] = sum;
	return sum;
}

int main() {
	int n;
	cin >> n;
	g.resize(n);
	dp.resize(n);
	children.resize(n);
	rep(i, n - 1) {
		int v, w;
		cin >> v >> w;
		v--; w--;
		g[v].push_back(w);
		g[w].push_back(v);
	}
	dfs(0, -1);
	rep(i, n) {
		ll total = 0;
		ll cSum = 0;
		for (auto child : children[i]){
			total += child * cSum * 2;
			cSum  += child;
		}
		total += cSum * 2 + 1;
		cout << total << endl;
	}
	return 0;
}
0