結果

問題 No.1098 LCAs
ユーザー kyoprounokyoprouno
提出日時 2020-06-27 00:56:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 604 ms / 2,000 ms
コード長 1,467 bytes
コンパイル時間 2,023 ms
コンパイル使用メモリ 178,704 KB
実行使用メモリ 23,928 KB
最終ジャッジ日時 2023-09-18 12:29:50
合計ジャッジ時間 10,998 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
9,100 KB
testcase_01 AC 5 ms
9,100 KB
testcase_02 AC 5 ms
9,156 KB
testcase_03 AC 5 ms
9,324 KB
testcase_04 AC 5 ms
9,184 KB
testcase_05 AC 5 ms
9,088 KB
testcase_06 AC 5 ms
9,416 KB
testcase_07 AC 5 ms
9,128 KB
testcase_08 AC 5 ms
9,132 KB
testcase_09 AC 5 ms
9,452 KB
testcase_10 AC 5 ms
9,260 KB
testcase_11 AC 5 ms
9,116 KB
testcase_12 AC 5 ms
9,128 KB
testcase_13 AC 7 ms
9,232 KB
testcase_14 AC 7 ms
9,184 KB
testcase_15 AC 7 ms
9,184 KB
testcase_16 AC 7 ms
9,208 KB
testcase_17 AC 7 ms
9,200 KB
testcase_18 AC 537 ms
18,620 KB
testcase_19 AC 528 ms
18,628 KB
testcase_20 AC 533 ms
18,596 KB
testcase_21 AC 536 ms
18,592 KB
testcase_22 AC 533 ms
18,704 KB
testcase_23 AC 432 ms
18,548 KB
testcase_24 AC 428 ms
18,532 KB
testcase_25 AC 420 ms
18,552 KB
testcase_26 AC 462 ms
18,504 KB
testcase_27 AC 444 ms
18,536 KB
testcase_28 AC 602 ms
23,660 KB
testcase_29 AC 604 ms
23,928 KB
testcase_30 AC 596 ms
23,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))

using namespace std;

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;}

const ll INF=1e18;

vector<ll> to[200100];
ll par[200100];
ll dp[200100];

void dfs1(ll v,ll p=-1){
    par[v]=p;
    for(auto x:to[v]){
        if(x==p) continue;
        dfs1(x,v);
    }
    return ;
}

void dfs(ll v,ll p=-1){
    ll res=0;
    for(auto x:to[v]){
        if(x==p) continue;
        dfs(x,v);
        res+=dp[x];
    }
    dp[v]=res+1;
    return;
}

int main(){
    ll n;
    cin >> n;
    rep(i,n-1){
        ll v,w;
        cin >> v >> w;
        v--; w--;
        to[v].push_back(w);
        to[w].push_back(v);
    }
        
    dfs1(0);
    dfs(0);
    rep(i,n){
        //cout << dp[i] << endl;
    }
    rep(i,n){
        ll s=0;
        ll t=0;
        for(auto x:to[i]){
            if(x==par[i]) continue;
            s+=dp[x];
            t+=dp[x]*dp[x];
        }
        ll ans=(s+1)*(s+1)-t;
        cout << ans << endl;
    }
}
0