結果

問題 No.1098 LCAs
ユーザー takakintakakin
提出日時 2020-06-27 17:27:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,743 ms / 2,000 ms
コード長 912 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 76,032 KB
最終ジャッジ日時 2024-05-05 13:30:33
合計ジャッジ時間 23,432 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
11,008 KB
testcase_01 AC 28 ms
11,008 KB
testcase_02 AC 30 ms
11,008 KB
testcase_03 AC 27 ms
10,752 KB
testcase_04 AC 26 ms
11,008 KB
testcase_05 AC 28 ms
11,008 KB
testcase_06 AC 27 ms
10,880 KB
testcase_07 AC 27 ms
10,752 KB
testcase_08 AC 26 ms
11,008 KB
testcase_09 AC 27 ms
10,880 KB
testcase_10 AC 27 ms
10,880 KB
testcase_11 AC 26 ms
10,880 KB
testcase_12 AC 25 ms
10,752 KB
testcase_13 AC 31 ms
11,008 KB
testcase_14 AC 33 ms
11,136 KB
testcase_15 AC 33 ms
11,264 KB
testcase_16 AC 32 ms
11,008 KB
testcase_17 AC 33 ms
11,008 KB
testcase_18 AC 1,673 ms
56,064 KB
testcase_19 AC 1,673 ms
56,064 KB
testcase_20 AC 1,696 ms
56,320 KB
testcase_21 AC 1,683 ms
56,192 KB
testcase_22 AC 1,627 ms
56,064 KB
testcase_23 AC 1,322 ms
51,768 KB
testcase_24 AC 1,299 ms
51,672 KB
testcase_25 AC 1,235 ms
50,172 KB
testcase_26 AC 1,322 ms
56,700 KB
testcase_27 AC 1,330 ms
56,452 KB
testcase_28 AC 1,738 ms
75,776 KB
testcase_29 AC 1,743 ms
76,032 KB
testcase_30 AC 1,738 ms
75,776 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