結果

問題 No.1098 LCAs
ユーザー QDSNQDSN
提出日時 2020-07-05 00:58:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 577 ms / 2,000 ms
コード長 1,063 bytes
コンパイル時間 1,810 ms
コンパイル使用メモリ 175,852 KB
実行使用メモリ 32,072 KB
最終ジャッジ日時 2023-10-19 22:09:28
合計ジャッジ時間 9,984 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 4 ms
4,348 KB
testcase_14 AC 4 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 4 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 516 ms
17,552 KB
testcase_19 AC 516 ms
17,552 KB
testcase_20 AC 515 ms
17,552 KB
testcase_21 AC 511 ms
17,552 KB
testcase_22 AC 518 ms
17,552 KB
testcase_23 AC 421 ms
18,120 KB
testcase_24 AC 417 ms
18,124 KB
testcase_25 AC 408 ms
18,344 KB
testcase_26 AC 443 ms
18,344 KB
testcase_27 AC 436 ms
18,344 KB
testcase_28 AC 577 ms
30,752 KB
testcase_29 AC 577 ms
32,072 KB
testcase_30 AC 574 ms
30,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
vector<ll> num;
vector<ll> ans;
vector<vector<int>> edge;
int dfs(int now, int prev) {
    int sum = 0;
    for(auto next : edge[now]) {
        if(next != prev) {
            int tmp = dfs(next, now);
            num[next] = tmp;
            sum += tmp;
        }
    }
    return sum + 1;
}
ll solve(int now, int prev) {
    ll res = num[now] * num[now];
    ll sub = 0;
    for(auto next : edge[now]) {
        if(next != prev) {
            ll tmp = solve(next, now);
            sub += tmp;
        }
    }
    ans[now] = res - sub;
    return res;
}
int main() {
    int n;
    cin >> n;
    edge.resize(n);
    for(int i = 0; i < n - 1; i++) {
        int v, w;
        cin >> v >> w;
        edge[v - 1].push_back(w - 1);
        edge[w - 1].push_back(v - 1);
    }
    num.resize(n, -1);
    ans.resize(n, 0);
    num[0] = dfs(0, -1);
    solve(0, -1);
    for(int i = 0; i < n; i++) {
        cout << ans[i] << endl;
    }
}
0