結果

問題 No.1098 LCAs
ユーザー itohdakitohdak
提出日時 2020-07-03 16:16:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 136 ms / 2,000 ms
コード長 1,017 bytes
コンパイル時間 1,927 ms
コンパイル使用メモリ 171,552 KB
実行使用メモリ 29,084 KB
最終ジャッジ日時 2023-10-14 23:36:28
合計ジャッジ時間 5,484 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,352 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,352 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 110 ms
17,220 KB
testcase_19 AC 112 ms
17,176 KB
testcase_20 AC 103 ms
17,296 KB
testcase_21 AC 102 ms
17,356 KB
testcase_22 AC 108 ms
17,160 KB
testcase_23 AC 80 ms
17,876 KB
testcase_24 AC 80 ms
17,788 KB
testcase_25 AC 70 ms
18,052 KB
testcase_26 AC 78 ms
18,060 KB
testcase_27 AC 75 ms
17,964 KB
testcase_28 AC 120 ms
28,284 KB
testcase_29 AC 136 ms
29,084 KB
testcase_30 AC 124 ms
28,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,n-1,0)
#define all(v) v.begin(), v.end()
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N; cin >> N;
  vector<vector<int>> G(N);
  rep(i, N-1) {
    int v, w; cin >> v >> w; v--; w--;
    G[v].push_back(w);
    G[w].push_back(v);
  }
  vector<ll> nChild(N);
  vector<ll> ans(N);
  ll tmp = 0;
  auto dfs = [&](auto dfs, int cur, int par) -> void {
    tmp++;
    ll in = tmp;
    ll same = 0;
    for(int ne: G[cur]) {
      if(ne != par) {
        dfs(dfs, ne, cur);
        same += nChild[ne] * nChild[ne];
      }
    }
    ll out = tmp;
    nChild[cur] = out-in+1;
    ans[cur] = nChild[cur] * nChild[cur] - same;
  };
  dfs(dfs, 0, -1);
  rep(i, N) cout << ans[i] << "\n";
  return 0;
}
0