結果

問題 No.1098 LCAs
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-03-06 01:21:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 446 ms / 2,000 ms
コード長 1,315 bytes
コンパイル時間 2,946 ms
コンパイル使用メモリ 212,288 KB
実行使用メモリ 27,076 KB
最終ジャッジ日時 2024-03-06 01:21:35
合計ジャッジ時間 11,384 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 2 ms
6,548 KB
testcase_04 AC 1 ms
6,548 KB
testcase_05 AC 2 ms
6,548 KB
testcase_06 AC 2 ms
6,548 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 1 ms
6,548 KB
testcase_10 AC 2 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 2 ms
6,548 KB
testcase_13 AC 3 ms
6,548 KB
testcase_14 AC 4 ms
6,548 KB
testcase_15 AC 3 ms
6,548 KB
testcase_16 AC 4 ms
6,548 KB
testcase_17 AC 3 ms
6,548 KB
testcase_18 AC 387 ms
19,268 KB
testcase_19 AC 412 ms
19,268 KB
testcase_20 AC 396 ms
19,268 KB
testcase_21 AC 385 ms
19,268 KB
testcase_22 AC 433 ms
19,268 KB
testcase_23 AC 344 ms
21,472 KB
testcase_24 AC 369 ms
21,596 KB
testcase_25 AC 333 ms
21,856 KB
testcase_26 AC 338 ms
21,856 KB
testcase_27 AC 344 ms
21,856 KB
testcase_28 AC 446 ms
26,436 KB
testcase_29 AC 417 ms
27,076 KB
testcase_30 AC 412 ms
26,308 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    ll N, A, B;
    cin >> N;
    vector<vector<ll>> E(N);
    for (int i=0; i < N-1; i++){
        cin >> A >> B;
        A--; B--;
        E[A].push_back(B);
        E[B].push_back(A);
    }

    //部分木のサイズ
    vector<int> subt(N);
    auto subtree_size=[&](auto self, int from, int p)->void{
        subt[from]=1;
        for (auto to : E[from]){
            if (to == p) continue;
            self(self, to, from); subt[from] += subt[to];
        }
    };
    subtree_size(subtree_size, 0, -1);

    //各頂点の親
    const int LOG=1;
    vector par(LOG, vector<ll>(N, -1));
    auto ancestor=[&](auto self, int from, int p)->void{
        for (auto to : E[from]){
            if (to == p) continue;
            par[0][to] = from;
            self(self, to, from);
        }
    };
    ancestor(ancestor, 0, -1);

    for (int i=0; i<N; i++){
        ll S = 0, ans=0;
        vector<ll> v;
        for (auto x : E[i]){
            if (x != par[0][i]){
                v.push_back(subt[x]);
                S += subt[x];
            }
        }
        for (auto x : v) ans += x*(S-x);
        cout << ans + subt[i]*2-1 << endl;
    }

    return 0;
}
0