結果

問題 No.1098 LCAs
ユーザー IKyoproIKyopro
提出日時 2020-06-26 21:28:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 882 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 206,716 KB
実行使用メモリ 27,840 KB
最終ジャッジ日時 2023-09-18 03:03:49
合計ジャッジ時間 7,015 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 161 ms
17,276 KB
testcase_19 AC 161 ms
17,300 KB
testcase_20 AC 161 ms
17,144 KB
testcase_21 AC 158 ms
17,364 KB
testcase_22 AC 160 ms
17,188 KB
testcase_23 AC 113 ms
17,728 KB
testcase_24 AC 112 ms
17,736 KB
testcase_25 AC 102 ms
18,092 KB
testcase_26 AC 108 ms
18,240 KB
testcase_27 AC 105 ms
18,032 KB
testcase_28 AC 186 ms
27,036 KB
testcase_29 AC 193 ms
27,840 KB
testcase_30 AC 191 ms
26,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vvec<int> g(N);
    for(int i=0;i<N-1;i++){
        int a,b;
        cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vec<ll> ans(N),s(N);
    auto dfs = [&](auto&& self,int cur,int par)->void{
        ll sum = 0,sum2 = 0;
        for(auto& to:g[cur]) if(to!=par){
            self(self,to,cur);
            sum += s[to];
            sum2 += s[to]*s[to];
            s[cur] += s[to];
        }
        s[cur]++;
        ans[cur] = (sum*sum-sum2)+2*s[cur]-1;
    };
    dfs(dfs,0,-1);
    for(int i=0;i<N;i++) cout << ans[i] << "\n";
}
0