結果

問題 No.1098 LCAs
ユーザー MayimgMayimg
提出日時 2020-07-03 14:34:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 754 bytes
コンパイル時間 2,583 ms
コンパイル使用メモリ 205,188 KB
実行使用メモリ 35,980 KB
最終ジャッジ日時 2023-10-14 23:31:00
合計ジャッジ時間 9,001 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,352 KB
testcase_14 AC 2 ms
4,356 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 3 ms
4,352 KB
testcase_18 AC 167 ms
16,068 KB
testcase_19 AC 169 ms
15,984 KB
testcase_20 AC 168 ms
15,860 KB
testcase_21 AC 171 ms
15,652 KB
testcase_22 AC 166 ms
15,892 KB
testcase_23 AC 106 ms
17,800 KB
testcase_24 AC 106 ms
17,792 KB
testcase_25 AC 95 ms
18,068 KB
testcase_26 AC 103 ms
18,016 KB
testcase_27 AC 99 ms
18,004 KB
testcase_28 AC 206 ms
34,464 KB
testcase_29 AC 200 ms
35,980 KB
testcase_30 AC 195 ms
34,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> g;
vector<long long> ans;
int dfs (int cur, int par) {
  vector<int> v;
  for (int child : g[cur]) if (child != par) {
    v.push_back(dfs(child, cur));
  }
  int tot = accumulate(v.begin(), v.end(), 0);
  for (auto& i : v) ans[cur] += 1LL * i * (tot - i);
  ans[cur] += 2LL * tot;
  ans[cur]++;
  return tot + 1;
}
signed main() { 
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  g = vector<vector<int>>(n);
  ans = vector<long long>(n);
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0, -1);
  for (auto& i : ans) cout << i << '\n';
  return 0;
}
0