結果

問題 No.1098 LCAs
ユーザー qibqib
提出日時 2023-05-17 16:54:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 668 bytes
コンパイル時間 696 ms
コンパイル使用メモリ 86,688 KB
実行使用メモリ 150,040 KB
最終ジャッジ日時 2023-08-21 20:21:08
合計ジャッジ時間 12,250 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,168 KB
testcase_01 AC 71 ms
71,356 KB
testcase_02 AC 71 ms
71,180 KB
testcase_03 AC 72 ms
71,268 KB
testcase_04 AC 71 ms
71,176 KB
testcase_05 AC 71 ms
70,940 KB
testcase_06 AC 70 ms
71,168 KB
testcase_07 AC 72 ms
71,344 KB
testcase_08 AC 73 ms
71,176 KB
testcase_09 AC 73 ms
71,092 KB
testcase_10 AC 72 ms
71,084 KB
testcase_11 AC 71 ms
71,260 KB
testcase_12 AC 71 ms
70,968 KB
testcase_13 AC 97 ms
76,204 KB
testcase_14 AC 96 ms
76,292 KB
testcase_15 AC 96 ms
76,468 KB
testcase_16 AC 97 ms
76,596 KB
testcase_17 AC 97 ms
76,428 KB
testcase_18 AC 536 ms
115,024 KB
testcase_19 AC 539 ms
115,096 KB
testcase_20 AC 536 ms
115,040 KB
testcase_21 AC 545 ms
115,056 KB
testcase_22 AC 548 ms
115,272 KB
testcase_23 AC 461 ms
143,880 KB
testcase_24 AC 443 ms
144,032 KB
testcase_25 AC 407 ms
146,924 KB
testcase_26 AC 441 ms
149,640 KB
testcase_27 AC 434 ms
150,040 KB
testcase_28 AC 539 ms
119,132 KB
testcase_29 AC 542 ms
119,240 KB
testcase_30 AC 533 ms
117,604 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())

g = [[] for _ in range(n)]
for _ in range(n - 1):
  u, v = map(int, input().split())
  u -= 1
  v -= 1
  g[u].append(v)
  g[v].append(u)

cnt = [0 for _ in range(n)]
src = 0
par = [None for _ in range(n)]
st = [~ src, src]
while len(st) > 0:
  cur = st.pop()
  if cur >= 0:
    for nxt in g[cur]:
      if nxt == par[cur]:
        continue
      par[nxt] = cur
      st.append(~ nxt)
      st.append(nxt)
  else:
    cnt[~ cur] += 1
    if not par[~ cur] is None:
      cnt[par[~ cur]] += cnt[~ cur]

ans = [0 for _ in range(n)]
for v in range(n):
  if not par[v] is None:
    ans[par[v]] -= cnt[v] ** 2
  ans[v] += cnt[v] ** 2

print(*ans, sep='\n')
0