結果

問題 No.1098 LCAs
ユーザー kappybarkappybar
提出日時 2020-06-26 22:51:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 957 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 173,528 KB
実行使用メモリ 30,112 KB
最終ジャッジ日時 2023-09-18 06:46:56
合計ジャッジ時間 9,970 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 441 ms
15,556 KB
testcase_19 AC 438 ms
15,712 KB
testcase_20 AC 440 ms
15,628 KB
testcase_21 AC 433 ms
15,744 KB
testcase_22 AC 441 ms
15,552 KB
testcase_23 AC 400 ms
16,152 KB
testcase_24 AC 392 ms
16,164 KB
testcase_25 AC 385 ms
16,348 KB
testcase_26 AC 414 ms
16,288 KB
testcase_27 AC 411 ms
16,384 KB
testcase_28 AC 474 ms
28,848 KB
testcase_29 AC 481 ms
30,112 KB
testcase_30 AC 484 ms
28,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
int n;
vector<vector<int>> tree(n,vector<int>());
vector<ll> ans(n,0);

pll dfs(int i,int before=-1){
    ll res = 0;
    ll sz = 1;

    for(int j:tree[i]){
        if(j==before) continue;
        pll p =  dfs(j,i);
        sz += p.first;
        res += p.second;
    }

    ll q = sz*sz;
    ans[i] = q - res;
    return pll(sz,res + ans[i]);
}

int main(){
    int n;
    cin >> n;
    tree.resize(n);
    ans.resize(n);
    rep(i,n-1){
        int u,v;
        cin >> u >> v;
        --u;--v;
        tree[u].push_back(v);
        tree[v].push_back(u);
    }
    dfs(0);
    rep(i,n){
        cout << ans[i] << endl;
    }
    return 0;
}
0