結果

問題 No.1098 LCAs
ユーザー Ricky_ponRicky_pon
提出日時 2020-06-26 22:05:39
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 2,489 ms
コンパイル使用メモリ 199,256 KB
実行使用メモリ 32,112 KB
最終ジャッジ日時 2023-09-18 04:42:43
合計ジャッジ時間 6,443 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,244 KB
testcase_01 AC 4 ms
9,300 KB
testcase_02 AC 5 ms
9,340 KB
testcase_03 AC 4 ms
9,324 KB
testcase_04 AC 4 ms
9,360 KB
testcase_05 AC 4 ms
9,396 KB
testcase_06 AC 5 ms
9,300 KB
testcase_07 AC 4 ms
9,252 KB
testcase_08 AC 5 ms
9,540 KB
testcase_09 AC 4 ms
9,300 KB
testcase_10 AC 4 ms
9,292 KB
testcase_11 AC 4 ms
9,300 KB
testcase_12 AC 4 ms
9,328 KB
testcase_13 AC 4 ms
9,468 KB
testcase_14 AC 5 ms
9,384 KB
testcase_15 AC 4 ms
9,348 KB
testcase_16 AC 5 ms
9,392 KB
testcase_17 AC 5 ms
9,372 KB
testcase_18 AC 167 ms
17,804 KB
testcase_19 AC 163 ms
17,864 KB
testcase_20 AC 168 ms
17,924 KB
testcase_21 AC 171 ms
17,932 KB
testcase_22 AC 166 ms
17,820 KB
testcase_23 AC 114 ms
17,936 KB
testcase_24 AC 113 ms
17,920 KB
testcase_25 AC 104 ms
17,708 KB
testcase_26 AC 114 ms
17,868 KB
testcase_27 AC 108 ms
17,868 KB
testcase_28 AC 199 ms
30,900 KB
testcase_29 AC 207 ms
32,112 KB
testcase_30 AC 202 ms
30,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int n;
vector<int> G[MAX];
lint sz[MAX], ans[MAX];

void dfs(int v, int pv){
    sz[v] = 1;
    lint tmp = 0;
    for(int nv: G[v])if(nv != pv){
        dfs(nv, v);
        sz[v] += sz[nv];
        tmp += sz[nv] * sz[nv];
    }
    ans[v] = (sz[v]+1)*(sz[v]-1) - tmp + 1;
}

int main(){
    scanf("%d", &n);
    rep(i, n-1){
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    dfs(0, -1);
    rep(i, n) printf("%lld\n", ans[i]);
}
0