結果

問題 No.1098 LCAs
ユーザー auauaauaua
提出日時 2020-06-26 22:30:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 252 ms / 2,000 ms
コード長 974 bytes
コンパイル時間 1,751 ms
コンパイル使用メモリ 170,408 KB
実行使用メモリ 34,716 KB
最終ジャッジ日時 2023-09-18 05:56:30
合計ジャッジ時間 6,464 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
10,792 KB
testcase_01 AC 6 ms
10,904 KB
testcase_02 AC 6 ms
10,664 KB
testcase_03 AC 6 ms
10,764 KB
testcase_04 AC 6 ms
10,964 KB
testcase_05 AC 6 ms
10,708 KB
testcase_06 AC 7 ms
10,828 KB
testcase_07 AC 5 ms
10,596 KB
testcase_08 AC 5 ms
10,724 KB
testcase_09 AC 6 ms
10,836 KB
testcase_10 AC 7 ms
10,908 KB
testcase_11 AC 7 ms
10,672 KB
testcase_12 AC 6 ms
10,896 KB
testcase_13 AC 7 ms
10,592 KB
testcase_14 AC 6 ms
10,836 KB
testcase_15 AC 6 ms
10,596 KB
testcase_16 AC 6 ms
10,836 KB
testcase_17 AC 7 ms
10,848 KB
testcase_18 AC 215 ms
18,276 KB
testcase_19 AC 207 ms
18,204 KB
testcase_20 AC 213 ms
18,336 KB
testcase_21 AC 214 ms
18,224 KB
testcase_22 AC 202 ms
18,220 KB
testcase_23 AC 152 ms
18,596 KB
testcase_24 AC 157 ms
18,668 KB
testcase_25 AC 146 ms
18,808 KB
testcase_26 AC 181 ms
18,672 KB
testcase_27 AC 169 ms
18,800 KB
testcase_28 AC 252 ms
33,008 KB
testcase_29 AC 249 ms
34,716 KB
testcase_30 AC 246 ms
33,124 KB
権限があれば一括ダウンロードができます

ソースコード

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