結果

問題 No.1098 LCAs
ユーザー betrue12betrue12
提出日時 2020-06-26 21:41:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 499 ms / 2,000 ms
コード長 644 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 204,080 KB
実行使用メモリ 23,620 KB
最終ジャッジ日時 2023-09-18 03:40:49
合計ジャッジ時間 9,904 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,092 KB
testcase_01 AC 4 ms
8,100 KB
testcase_02 AC 4 ms
8,024 KB
testcase_03 AC 4 ms
8,048 KB
testcase_04 AC 4 ms
8,068 KB
testcase_05 AC 3 ms
8,008 KB
testcase_06 AC 4 ms
8,100 KB
testcase_07 AC 4 ms
8,056 KB
testcase_08 AC 3 ms
8,052 KB
testcase_09 AC 4 ms
8,252 KB
testcase_10 AC 3 ms
8,024 KB
testcase_11 AC 3 ms
8,024 KB
testcase_12 AC 4 ms
8,012 KB
testcase_13 AC 6 ms
8,140 KB
testcase_14 AC 7 ms
8,056 KB
testcase_15 AC 6 ms
8,256 KB
testcase_16 AC 6 ms
8,216 KB
testcase_17 AC 6 ms
8,068 KB
testcase_18 AC 454 ms
17,496 KB
testcase_19 AC 458 ms
17,192 KB
testcase_20 AC 458 ms
17,216 KB
testcase_21 AC 455 ms
17,220 KB
testcase_22 AC 465 ms
17,308 KB
testcase_23 AC 395 ms
17,760 KB
testcase_24 AC 396 ms
17,916 KB
testcase_25 AC 383 ms
17,920 KB
testcase_26 AC 416 ms
18,136 KB
testcase_27 AC 409 ms
17,936 KB
testcase_28 AC 497 ms
23,020 KB
testcase_29 AC 497 ms
23,620 KB
testcase_30 AC 499 ms
22,956 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;
    cin >> N;
    vector<int> edges[200000];
    for(int i=0; i<N-1; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }

    vector<int64_t> sum(N), ans(N);
    auto dfs = [&](auto&& dfs, int i, int p)->void{
        for(int j : edges[i]) if(j != p){
            dfs(dfs, j, i);
            sum[i] += sum[j];
            ans[i] -= sum[j]*sum[j];
        }
        sum[i]++;
        ans[i] += sum[i]*sum[i];
    };
    dfs(dfs, 0, -1);
    for(auto a : ans) cout << a << endl;
    return 0;
}
0