結果
問題 | No.1098 LCAs |
ユーザー | どらら |
提出日時 | 2020-06-26 23:16:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 558 ms / 2,000 ms |
コード長 | 913 bytes |
コンパイル時間 | 1,765 ms |
コンパイル使用メモリ | 168,968 KB |
実行使用メモリ | 40,004 KB |
最終ジャッジ日時 | 2024-07-04 23:52:19 |
合計ジャッジ時間 | 9,632 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; vector<int> G[200005]; int cnt[200005]; ll ans[200005]; int dfs(int v, int p){ int sum = 0; vector<int> tmp; rep(i,G[v].size()){ if(G[v][i] == p) continue; int res = dfs(G[v][i], v); tmp.push_back(res); sum += res; } ll x = sum; rep(i,tmp.size()){ ll a = tmp[i]; ll b = sum - tmp[i] + 1; x += a * b; } ans[v] = x + 1; return cnt[v] = sum + 1; } int main(){ int N; cin >> N; rep(i,N-1){ int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } dfs(1, -1); REP(i,1,N+1){ cout << ans[i] << endl; } return 0; }