結果

問題 No.1098 LCAs
ユーザー 👑 jupirojupiro
提出日時 2020-06-26 21:49:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 1,893 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 134,428 KB
実行使用メモリ 31,740 KB
最終ジャッジ日時 2023-09-18 04:08:35
合計ジャッジ時間 5,540 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 185 ms
17,348 KB
testcase_19 AC 181 ms
17,220 KB
testcase_20 AC 191 ms
17,284 KB
testcase_21 AC 194 ms
17,324 KB
testcase_22 AC 193 ms
17,468 KB
testcase_23 AC 120 ms
17,720 KB
testcase_24 AC 121 ms
17,776 KB
testcase_25 AC 108 ms
17,944 KB
testcase_26 AC 119 ms
17,884 KB
testcase_27 AC 114 ms
17,944 KB
testcase_28 AC 248 ms
30,472 KB
testcase_29 AC 252 ms
31,740 KB
testcase_30 AC 241 ms
30,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;
const long double PI = acos((long double)(-1));

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

int n;
vector<vector<int>> g;
vector<ll> sz;
vector<ll> res;
void dfs_sz(int cur, int pre) {
	sz[cur] = 1;
	for (auto nxt : g[cur]) {
		if (pre == nxt) continue;
		dfs_sz(nxt, cur);
		sz[cur] += sz[nxt];
	}
}

void dfs(int cur, int pre) {
	ll all = 0;
	for (auto nxt : g[cur]) {
		if (nxt == pre) continue;
		all += sz[nxt];
	}
	res[cur] += (sz[cur] - 1) * 2 + 1;
	for (auto nxt : g[cur]) {
		if (nxt == pre) continue;
		ll t = all - sz[nxt];
		res[cur] += sz[nxt] * t;
	}
	for (auto nxt : g[cur]) {
		if (nxt == pre) continue;
		dfs(nxt, cur);
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	scanf("%d", &n);
	g.resize(n); sz.resize(n); res.resize(n);
	for (int i = 0; i < n - 1; i++) {
		int u, v; scanf("%d %d", &u, &v);
		u--; v--;
		g[u].emplace_back(v);
		g[v].emplace_back(u);
	}
	dfs_sz(0, -1);
	dfs(0, -1);
	for (int i = 0; i < n; i++) printf("%lld\n", res[i]);
	return 0;
}
0