結果

問題 No.1098 LCAs
ユーザー takakintakakin
提出日時 2020-06-27 17:27:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 640 ms / 2,000 ms
コード長 912 bytes
コンパイル時間 871 ms
コンパイル使用メモリ 87,176 KB
実行使用メモリ 130,784 KB
最終ジャッジ日時 2023-09-19 05:40:47
合計ジャッジ時間 11,885 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,488 KB
testcase_01 AC 93 ms
71,216 KB
testcase_02 AC 95 ms
71,340 KB
testcase_03 AC 93 ms
71,636 KB
testcase_04 AC 93 ms
71,712 KB
testcase_05 AC 94 ms
71,548 KB
testcase_06 AC 94 ms
71,636 KB
testcase_07 AC 95 ms
71,680 KB
testcase_08 AC 94 ms
71,560 KB
testcase_09 AC 93 ms
71,616 KB
testcase_10 AC 93 ms
71,852 KB
testcase_11 AC 94 ms
71,656 KB
testcase_12 AC 94 ms
71,480 KB
testcase_13 AC 119 ms
77,596 KB
testcase_14 AC 121 ms
77,444 KB
testcase_15 AC 122 ms
77,640 KB
testcase_16 AC 119 ms
77,616 KB
testcase_17 AC 122 ms
77,448 KB
testcase_18 AC 640 ms
120,660 KB
testcase_19 AC 593 ms
122,192 KB
testcase_20 AC 608 ms
122,212 KB
testcase_21 AC 633 ms
122,268 KB
testcase_22 AC 636 ms
122,320 KB
testcase_23 AC 467 ms
129,760 KB
testcase_24 AC 463 ms
129,068 KB
testcase_25 AC 413 ms
130,784 KB
testcase_26 AC 469 ms
129,424 KB
testcase_27 AC 480 ms
129,292 KB
testcase_28 AC 629 ms
120,420 KB
testcase_29 AC 628 ms
123,320 KB
testcase_30 AC 633 ms
119,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
edge=[[] for i in range(n)]
for i in range(n-1):
  a,b=map(int,input().split())
  edge[a-1].append(b-1)
  edge[b-1].append(a-1)

inf=float("inf")
Ans=[0]*n
Acm=[0]*n
Par=[inf]*n
Par[0]=-1
Deg=[0]*n
Deg[0]=0
Chk=[0]
while Chk:
  c=Chk.pop()
  for next in edge[c]:
    if Par[next]==inf:
      Par[next]=c
      Deg[next]+=1
      Chk.append(next)

from collections import deque
TS=list(v for v in range(n) if Deg[v]==0)
D=deque(TS)
while D:
  v=D.popleft()
  for t in edge[v]:
    if t!=Par[v]:
      Deg[t]-=1
      if Deg[t]==0:
        D.append(t)
        TS.append(t)

Used=[False]*n
C=[0]*n
for i in reversed(range(n)):
  v=TS[i]
  if Par[v]!=-1:
    C[Par[v]]+=C[v]+1

for i in reversed(range(n)):
  v=TS[i]
  Ans[v]+=(C[v]+1)**2
  Acm[v]+=Ans[v]
  if Par[v]!=-1:
    Ans[Par[v]]-=Acm[v]
    Acm[Par[v]]+=Acm[v]

for a in Ans:
  print(a)
0