結果
問題 | No.1098 LCAs |
ユーザー |
|
提出日時 | 2020-06-26 21:58:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 279 ms / 2,000 ms |
コード長 | 1,012 bytes |
コンパイル時間 | 1,699 ms |
コンパイル使用メモリ | 172,228 KB |
実行使用メモリ | 28,160 KB |
最終ジャッジ日時 | 2024-07-04 21:02:41 |
合計ジャッジ時間 | 6,453 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
#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 secondconstexpr char ln = '\n';const int mx=200010;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;}