結果

問題 No.1098 LCAs
ユーザー auaua
提出日時 2020-06-26 22:30:38
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 974 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 171,028 KB
実行使用メモリ 34,784 KB
最終ジャッジ日時 2024-07-04 22:15:39
合計ジャッジ時間 6,129 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define double long double
#define endl '\n'
typedef long long ll;
typedef pair<ll,ll> pll;
const ll inf=1e9+7;
const ll mod=1e9+7;
vector<vector<ll> >G(200010);
vector<ll>dp(200010,-1);

vector<ll>ans(200010);
ll dfs(ll i,ll p){
    if(dp[i]>=0)return dp[i];
    ll sum=0;
    for(auto e:G[i]){
        if(e==p)continue;
        sum+=dfs(e,i);
    }
    for(auto e:G[i]){
        if(e==p)continue;
        ans[i]+=(sum-dp[e])*(dp[e]);
    }
    ans[i]+=sum*2;
    ans[i]++;
    sum++;
    dp[i]=sum;
    return sum;
}
signed main(){
    ll n;cin>>n;
    rep(i,n-1){
        ll a,b;cin>>a>>b;
        a--;b--;
        G[a].pb(b);
        G[b].pb(a);
    }
    dfs(0,-1);
    rep(i,n){
        cout<<ans[i]<<endl;
    }
}
0