結果

問題 No.1098 LCAs
ユーザー face4face4
提出日時 2020-07-24 15:59:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 591 ms / 2,000 ms
コード長 744 bytes
コンパイル時間 794 ms
コンパイル使用メモリ 71,508 KB
実行使用メモリ 43,520 KB
最終ジャッジ日時 2023-09-07 13:04:37
合計ジャッジ時間 9,322 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,720 KB
testcase_01 AC 6 ms
12,820 KB
testcase_02 AC 6 ms
12,720 KB
testcase_03 AC 6 ms
12,704 KB
testcase_04 AC 6 ms
12,764 KB
testcase_05 AC 6 ms
12,724 KB
testcase_06 AC 6 ms
12,776 KB
testcase_07 AC 6 ms
12,704 KB
testcase_08 AC 6 ms
12,728 KB
testcase_09 AC 6 ms
12,736 KB
testcase_10 AC 6 ms
12,732 KB
testcase_11 AC 6 ms
12,912 KB
testcase_12 AC 6 ms
12,732 KB
testcase_13 AC 8 ms
12,780 KB
testcase_14 AC 8 ms
12,768 KB
testcase_15 AC 8 ms
12,780 KB
testcase_16 AC 8 ms
12,820 KB
testcase_17 AC 8 ms
12,772 KB
testcase_18 AC 511 ms
23,028 KB
testcase_19 AC 514 ms
23,032 KB
testcase_20 AC 505 ms
22,960 KB
testcase_21 AC 511 ms
22,840 KB
testcase_22 AC 514 ms
22,888 KB
testcase_23 AC 409 ms
21,908 KB
testcase_24 AC 415 ms
22,252 KB
testcase_25 AC 383 ms
21,896 KB
testcase_26 AC 418 ms
21,872 KB
testcase_27 AC 419 ms
21,820 KB
testcase_28 AC 569 ms
41,680 KB
testcase_29 AC 589 ms
43,520 KB
testcase_30 AC 591 ms
41,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
using namespace std;

vector<int> v[200000], child, way[200000];

void dfs(int p, int x){
    child[x] = 1;
    for(int c : v[x]){
        if(c == p)  continue;
        dfs(x, c);
        way[x].push_back(c);
        child[x] += child[c];
    }
}

typedef long long ll;

int main(){
    int n;
    cin >> n;
    child.resize(n, 0);
    for(int i = 0; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        a--, b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    dfs(-1, 0);
    for(int i = 0; i < n; i++){
        ll ans = 2*child[i]-1;
        for(int c : way[i]){
            ans += (ll)child[c]*(child[i]-1-child[c]);
        }
        cout << ans << endl;
    }
    return 0;
}
0