結果

問題 No.1098 LCAs
ユーザー hedwig100hedwig100
提出日時 2020-06-26 22:41:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 503 ms / 2,000 ms
コード長 903 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 172,244 KB
実行使用メモリ 30,260 KB
最終ジャッジ日時 2023-09-18 06:20:43
合計ジャッジ時間 9,359 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 461 ms
15,880 KB
testcase_19 AC 473 ms
15,652 KB
testcase_20 AC 467 ms
15,620 KB
testcase_21 AC 465 ms
15,816 KB
testcase_22 AC 459 ms
15,712 KB
testcase_23 AC 393 ms
16,064 KB
testcase_24 AC 394 ms
16,068 KB
testcase_25 AC 389 ms
16,328 KB
testcase_26 AC 421 ms
16,344 KB
testcase_27 AC 412 ms
16,292 KB
testcase_28 AC 503 ms
28,788 KB
testcase_29 AC 495 ms
30,260 KB
testcase_30 AC 503 ms
29,064 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i ++)
using namespace std;
using ll = long long;
using PL = pair<ll,ll>;
using P = pair<int,int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

int N;
vector<vector<int>> G;
vector<ll> A;

int dfs(int v,int p = - 1) {
    int cnt = 1;
    ll ans = 1;
    for (int e:G[v]) {
        if (e == p) continue;
        int num = dfs(e,v);
        ans += (ll)cnt * num;
        cnt += num;
    }
    A[v] = 2*ans - 1;
    return cnt;
}

int main() {
    cin >> N;
    G.resize(N);
    A.resize(N);
    rep(i,N - 1) {
        int a,b; cin >> a >> b;
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(0);
    rep(i,N) {
        cout << A[i] << endl;
    }
    return 0;
}
0