結果

問題 No.1098 LCAs
ユーザー T1610T1610
提出日時 2020-06-30 13:01:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,230 bytes
コンパイル時間 1,738 ms
コンパイル使用メモリ 171,924 KB
実行使用メモリ 34,588 KB
最終ジャッジ日時 2023-10-01 00:19:12
合計ジャッジ時間 8,062 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 154 ms
17,164 KB
testcase_19 AC 154 ms
17,168 KB
testcase_20 AC 150 ms
17,408 KB
testcase_21 AC 150 ms
17,368 KB
testcase_22 AC 159 ms
17,168 KB
testcase_23 AC 103 ms
17,800 KB
testcase_24 AC 105 ms
17,784 KB
testcase_25 AC 98 ms
18,072 KB
testcase_26 AC 105 ms
18,096 KB
testcase_27 AC 101 ms
18,196 KB
testcase_28 AC 195 ms
33,060 KB
testcase_29 AC 185 ms
34,588 KB
testcase_30 AC 191 ms
33,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

template<typename T> T chmax(T& a, const T& b){return a = (a > b ? a : b);}
template<typename T> T chmin(T& a, const T& b){return a = (a < b ? a : b);}

vector<vi> es;
vl ans;
vl cnt;

void calc(int cur, int par) {
    cnt[cur] = 1;
    for(auto&& to : es[cur]) if(to != par) {
        calc(to, cur);
        cnt[cur] += cnt[to];
    }
    ans[cur] += cnt[cur];
    for(auto&& to : es[cur]) if(to != par) {
        ans[cur] += cnt[to] * (cnt[cur] - cnt[to]);
    }
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    es.resize(n);
    rep(i, n-1) {
        int a, b;
        cin >> a >> b;
        --a;--b;
        es[a].emplace_back(b);
        es[b].emplace_back(a);
    }
    ans.resize(n);
    cnt.resize(n);
    calc(0, -1);
    rep(i, n) cout << ans[i] << '\n';
    return 0;
}
0