結果

問題 No.1098 LCAs
ユーザー titia
提出日時 2020-06-26 23:15:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 679 ms / 2,000 ms
コード長 727 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 82,596 KB
実行使用メモリ 153,272 KB
最終ジャッジ日時 2024-07-04 23:51:27
合計ジャッジ時間 10,628 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

N=int(input())
E=[[] for i in range(N+1)]


for i in range(N-1):
    x,y=map(int,input().split())
    E[x].append(y)
    E[y].append(x)

Q=[1]
TOP_SORT=[]
USE=[0]*(N+1)
USE[1]=1
P=[-1]*(N+1)
while Q:
    x=Q.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        if USE[to]==0:
            USE[to]=1
            Q.append(to)
            P[to]=x

Children=[1]*(N+1)

for t in TOP_SORT[::-1]:

    if t!=1 and len(E[t])==1:
        Children[t]=1

    if t!=1:
        Children[P[t]]+=Children[t]

for i in range(1,N+1):
    ANS=Children[i]*2-1

    X=[]
    for to in E[i]:
        if to==P[i]:
            continue
        X.append(Children[to])

    S=sum(X)

    for x in X:
        ANS+=x*(S-x)

    print(ANS)

    
    

0