結果

問題 No.1098 LCAs
ユーザー どららどらら
提出日時 2020-06-26 23:16:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 488 ms / 2,000 ms
コード長 913 bytes
コンパイル時間 1,590 ms
コンパイル使用メモリ 167,136 KB
実行使用メモリ 39,900 KB
最終ジャッジ日時 2023-09-18 07:48:50
合計ジャッジ時間 8,987 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,508 KB
testcase_01 AC 3 ms
8,324 KB
testcase_02 AC 4 ms
8,352 KB
testcase_03 AC 3 ms
8,312 KB
testcase_04 AC 3 ms
8,336 KB
testcase_05 AC 3 ms
8,380 KB
testcase_06 AC 3 ms
8,320 KB
testcase_07 AC 3 ms
8,376 KB
testcase_08 AC 4 ms
8,312 KB
testcase_09 AC 3 ms
8,300 KB
testcase_10 AC 3 ms
8,480 KB
testcase_11 AC 3 ms
8,336 KB
testcase_12 AC 3 ms
8,448 KB
testcase_13 AC 5 ms
8,400 KB
testcase_14 AC 5 ms
8,364 KB
testcase_15 AC 5 ms
8,564 KB
testcase_16 AC 5 ms
8,372 KB
testcase_17 AC 5 ms
8,368 KB
testcase_18 AC 436 ms
16,824 KB
testcase_19 AC 450 ms
16,972 KB
testcase_20 AC 448 ms
16,880 KB
testcase_21 AC 432 ms
16,824 KB
testcase_22 AC 446 ms
16,876 KB
testcase_23 AC 393 ms
18,564 KB
testcase_24 AC 399 ms
18,616 KB
testcase_25 AC 382 ms
18,484 KB
testcase_26 AC 408 ms
18,480 KB
testcase_27 AC 404 ms
18,524 KB
testcase_28 AC 481 ms
38,072 KB
testcase_29 AC 488 ms
39,900 KB
testcase_30 AC 483 ms
37,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

vector<int> G[200005];
int cnt[200005];
ll ans[200005];

int dfs(int v, int p){
  int sum = 0;
  vector<int> tmp;
  rep(i,G[v].size()){
    if(G[v][i] == p) continue;
    int res = dfs(G[v][i], v);
    tmp.push_back(res);
    sum += res;
  }

  ll x = sum;
  rep(i,tmp.size()){
    ll a = tmp[i];
    ll b = sum - tmp[i] + 1;
    x += a * b;
  }
  ans[v] = x + 1;
  
  return cnt[v] = sum + 1;
}

int main(){
  int N;
  cin >> N;
  rep(i,N-1){
    int a, b;
    cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  dfs(1, -1);
  
  REP(i,1,N+1){
    cout << ans[i] << endl;
  }
  
  return 0;
}


0