結果

問題 No.1098 LCAs
ユーザー kappybarkappybar
提出日時 2020-06-26 22:49:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 958 bytes
コンパイル時間 1,853 ms
コンパイル使用メモリ 173,756 KB
実行使用メモリ 29,436 KB
最終ジャッジ日時 2023-09-18 06:43:16
合計ジャッジ時間 9,469 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 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,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4 ms
4,376 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 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<int> 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