結果

問題 No.1098 LCAs
ユーザー QDSNQDSN
提出日時 2020-07-05 00:57:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,065 bytes
コンパイル時間 1,718 ms
コンパイル使用メモリ 174,228 KB
実行使用メモリ 30,504 KB
最終ジャッジ日時 2023-10-19 22:08:30
合計ジャッジ時間 11,447 ms
ジャッジサーバーID
(参考情報)
judge14 / 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 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
vector<int> num;
vector<int> 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