結果

問題 No.1098 LCAs
ユーザー fastmathfastmath
提出日時 2020-06-26 21:42:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 1,078 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 168,272 KB
実行使用メモリ 51,680 KB
最終ジャッジ日時 2023-09-18 03:42:25
合計ジャッジ時間 5,467 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
30,304 KB
testcase_01 AC 11 ms
30,132 KB
testcase_02 AC 11 ms
30,152 KB
testcase_03 AC 11 ms
30,128 KB
testcase_04 AC 11 ms
30,320 KB
testcase_05 AC 11 ms
30,240 KB
testcase_06 AC 11 ms
30,200 KB
testcase_07 AC 10 ms
30,180 KB
testcase_08 AC 11 ms
30,128 KB
testcase_09 AC 11 ms
30,200 KB
testcase_10 AC 11 ms
30,180 KB
testcase_11 AC 11 ms
30,240 KB
testcase_12 AC 11 ms
30,192 KB
testcase_13 AC 11 ms
30,200 KB
testcase_14 AC 11 ms
30,240 KB
testcase_15 AC 11 ms
30,200 KB
testcase_16 AC 11 ms
30,292 KB
testcase_17 AC 11 ms
30,212 KB
testcase_18 AC 157 ms
41,244 KB
testcase_19 AC 157 ms
41,248 KB
testcase_20 AC 157 ms
41,248 KB
testcase_21 AC 164 ms
41,508 KB
testcase_22 AC 155 ms
41,240 KB
testcase_23 AC 109 ms
41,064 KB
testcase_24 AC 109 ms
41,256 KB
testcase_25 AC 101 ms
41,408 KB
testcase_26 AC 113 ms
41,540 KB
testcase_27 AC 108 ms
41,340 KB
testcase_28 AC 193 ms
50,760 KB
testcase_29 AC 189 ms
51,680 KB
testcase_30 AC 185 ms
50,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC

const int N = 1e6+7;
vector <int> tree[N];
int ans[N];
int cnt[N];
void dfs(int u, int p) {
    cnt[u] = 1;
    for (int v : tree[u]) { 
        if (v == p)
            continue;
        dfs(v, u);
        cnt[u] += cnt[v];
    }
    ans[u] = cnt[u] * cnt[u];
    for (int v : tree[u]) {
        if (v == p)
            continue;
        ans[u] -= cnt[v] * cnt[v];
    }   
}   

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    int n;
    cin >> n;
    for (int i = 1; i <= n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        tree[u].app(v); tree[v].app(u);
    }   
    dfs(1, 1);

    for (int i = 1; i <= n; ++i)
        cout << ans[i] << endl;
}
0