結果

問題 No.1098 LCAs
ユーザー ぷらぷら
提出日時 2021-03-18 22:54:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 522 ms / 2,000 ms
コード長 756 bytes
コンパイル時間 1,780 ms
コンパイル使用メモリ 171,240 KB
実行使用メモリ 36,288 KB
最終ジャッジ日時 2024-04-28 13:11:21
合計ジャッジ時間 10,487 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,192 KB
testcase_01 AC 4 ms
8,056 KB
testcase_02 AC 3 ms
8,148 KB
testcase_03 AC 3 ms
8,228 KB
testcase_04 AC 4 ms
8,184 KB
testcase_05 AC 4 ms
8,940 KB
testcase_06 AC 4 ms
8,136 KB
testcase_07 AC 4 ms
8,348 KB
testcase_08 AC 5 ms
8,688 KB
testcase_09 AC 5 ms
8,492 KB
testcase_10 AC 4 ms
8,180 KB
testcase_11 AC 4 ms
8,360 KB
testcase_12 AC 4 ms
8,168 KB
testcase_13 AC 6 ms
8,084 KB
testcase_14 AC 7 ms
8,688 KB
testcase_15 AC 7 ms
9,064 KB
testcase_16 AC 6 ms
8,316 KB
testcase_17 AC 6 ms
9,024 KB
testcase_18 AC 493 ms
16,060 KB
testcase_19 AC 500 ms
16,240 KB
testcase_20 AC 499 ms
16,200 KB
testcase_21 AC 493 ms
16,020 KB
testcase_22 AC 480 ms
16,040 KB
testcase_23 AC 396 ms
18,056 KB
testcase_24 AC 409 ms
17,964 KB
testcase_25 AC 389 ms
17,992 KB
testcase_26 AC 433 ms
18,084 KB
testcase_27 AC 419 ms
17,980 KB
testcase_28 AC 521 ms
34,564 KB
testcase_29 AC 522 ms
36,288 KB
testcase_30 AC 521 ms
34,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int N;
long long dp[200005];
vector<int>ki[200005];

int dfs(int n,int p) {
    vector<int>tmp;
    for(auto x:ki[n]) {
        if(x != p) {
            tmp.push_back(dfs(x,n));
        }
    }
    int ans = 1;
    for(int i = 0; i < tmp.size(); i++) {
        ans += tmp[i];
    }
    long long ans2 = ans;
    for(int i = 0; i < tmp.size(); i++) {
        ans2 += (long long)(ans-tmp[i])*tmp[i];
    }
    dp[n] = ans2;
    return ans;
}

int main() {
    cin >> N;
    for(int i = 0; i < N-1; i++) {
        int u,v;
        cin >> u >> v;
        u--;v--;
        ki[u].push_back(v);
        ki[v].push_back(u);
    }
    dfs(0,-1);
    for(int i = 0; i < N; i++) {
        cout << dp[i] << endl;
    }
}
0