結果
問題 | No.1098 LCAs |
ユーザー |
|
提出日時 | 2020-06-26 21:57:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,008 bytes |
コンパイル時間 | 1,714 ms |
コンパイル使用メモリ | 172,232 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-04 21:01:56 |
合計ジャッジ時間 | 5,903 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 10 RE * 18 |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; typedef long long ll; typedef long double ld; const int inf=1e9+7; const ll longinf=1LL<<60; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) #define F first #define S second constexpr char ln = '\n'; const int mx=10; const ll mod=1e9+7; vector<int> v[mx]; vector<int> ne(mx); vector<ll> cnt(mx,0); ll dfs(int x, int p){ ll tmp = 0; ne[x] = p; for(auto to:v[x]){ if(to==p) continue; tmp += dfs(to,x) + 1; } return cnt[x] = tmp; } int main(){ int n; cin >> n; rep(i,n-1){ int x,y; cin >> x >> y; x--; y--; v[x].emplace_back(y); v[y].emplace_back(x); } dfs(0,-1); rep(i,n){ vector<ll> a; ll sum = 0; for(auto to:v[i]){ if(to==ne[i]) continue; a.emplace_back(cnt[to]+1); sum += cnt[to]+1; } ll ans = 1; for(auto it:a){ ans += it*(sum-it); } ans += sum*2; cout << ans << ln; } return 0; }