結果

問題 No.1098 LCAs
ユーザー milanis48663220milanis48663220
提出日時 2020-06-26 22:18:02
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 440 ms / 2,000 ms
コード長 927 bytes
コンパイル時間 754 ms
コンパイル使用メモリ 84,868 KB
実行使用メモリ 29,264 KB
最終ジャッジ日時 2023-09-18 05:30:09
合計ジャッジ時間 7,828 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,604 KB
testcase_01 AC 4 ms
9,292 KB
testcase_02 AC 4 ms
9,316 KB
testcase_03 AC 4 ms
9,324 KB
testcase_04 AC 5 ms
9,288 KB
testcase_05 AC 4 ms
9,376 KB
testcase_06 AC 4 ms
9,316 KB
testcase_07 AC 4 ms
9,516 KB
testcase_08 AC 4 ms
9,288 KB
testcase_09 AC 4 ms
9,424 KB
testcase_10 AC 4 ms
9,296 KB
testcase_11 AC 4 ms
9,292 KB
testcase_12 AC 4 ms
9,388 KB
testcase_13 AC 6 ms
9,552 KB
testcase_14 AC 6 ms
9,428 KB
testcase_15 AC 6 ms
9,344 KB
testcase_16 AC 6 ms
9,420 KB
testcase_17 AC 6 ms
9,484 KB
testcase_18 AC 395 ms
18,136 KB
testcase_19 AC 406 ms
17,896 KB
testcase_20 AC 404 ms
17,848 KB
testcase_21 AC 402 ms
17,860 KB
testcase_22 AC 395 ms
17,952 KB
testcase_23 AC 341 ms
17,996 KB
testcase_24 AC 342 ms
18,144 KB
testcase_25 AC 339 ms
18,092 KB
testcase_26 AC 345 ms
17,972 KB
testcase_27 AC 342 ms
18,024 KB
testcase_28 AC 440 ms
28,336 KB
testcase_29 AC 427 ms
29,264 KB
testcase_30 AC 437 ms
28,256 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

ll N;
ll cnt[200000];
ll ans[200000];
vector<int> G[200000];
bool used[200000];

void dfs(int v){
    cnt[v] = 1;
    used[v] = true;
    ll sum = 0;
    ll sum_pow_2 = 0;
    for(int to : G[v]){
        if(!used[to]){
            dfs(to);
            cnt[v] += cnt[to];
            sum += cnt[to];
            sum_pow_2 += (cnt[to]*cnt[to]);
        }
    }
    ans[v] = (sum*sum-sum_pow_2);
    ans[v] += 2*cnt[v]-1;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N;
    for(int i = 0; i < N-1; i++){
        int u, v;
        cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(0);
    for(int i = 0; i < N; i++) cout << ans[i] << endl;
}
0