結果

問題 No.1098 LCAs
ユーザー totori_nyaatotori_nyaa
提出日時 2020-06-26 21:29:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 1,145 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 202,648 KB
実行使用メモリ 27,200 KB
最終ジャッジ日時 2023-09-18 03:04:53
合計ジャッジ時間 5,807 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,448 KB
testcase_01 AC 4 ms
7,684 KB
testcase_02 AC 4 ms
7,552 KB
testcase_03 AC 4 ms
7,676 KB
testcase_04 AC 3 ms
7,612 KB
testcase_05 AC 4 ms
7,756 KB
testcase_06 AC 3 ms
7,752 KB
testcase_07 AC 4 ms
7,608 KB
testcase_08 AC 3 ms
7,432 KB
testcase_09 AC 4 ms
7,548 KB
testcase_10 AC 4 ms
7,508 KB
testcase_11 AC 4 ms
7,504 KB
testcase_12 AC 4 ms
7,608 KB
testcase_13 AC 5 ms
7,684 KB
testcase_14 AC 5 ms
7,504 KB
testcase_15 AC 3 ms
7,624 KB
testcase_16 AC 5 ms
7,624 KB
testcase_17 AC 4 ms
7,988 KB
testcase_18 AC 128 ms
15,996 KB
testcase_19 AC 153 ms
15,648 KB
testcase_20 AC 145 ms
15,712 KB
testcase_21 AC 155 ms
15,592 KB
testcase_22 AC 159 ms
15,604 KB
testcase_23 AC 99 ms
16,160 KB
testcase_24 AC 100 ms
16,164 KB
testcase_25 AC 96 ms
16,452 KB
testcase_26 AC 100 ms
16,524 KB
testcase_27 AC 96 ms
16,376 KB
testcase_28 AC 214 ms
26,220 KB
testcase_29 AC 216 ms
27,200 KB
testcase_30 AC 209 ms
26,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
long double eps = 1e-6;
long double pi = acos(-1);

vector<vector<int>> v(200020);
ll ans[200020];

ll dfs(int p,int pre){
    ll ret = 1;
    for(auto i:v[p]){
        if(i != pre){
            ll cnt = dfs(i,p);
            ret += cnt;
        }
    }
    ans[p] = ret*(ret);
    return ret;
}
ll dfs2(int p,int pre){
    ll ret = ans[p];
    ll cnt = 0;
    for(auto i:v[p]){
        if(i!=pre){
            cnt = dfs2(i,p);
            ans[p] -= cnt;
        }
    }
    return ret;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        a--,b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(0,-1);
    dfs2(0,-1);
    for(int i=0;i<n;i++){
        cout << ans[i] << "\n";
    }
}
0