結果

問題 No.1103 Directed Length Sum
ユーザー asumo0729asumo0729
提出日時 2020-10-17 18:44:09
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 853 bytes
コンパイル時間 786 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 276,120 KB
最終ジャッジ日時 2023-09-28 08:29:41
合計ジャッジ時間 19,570 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,536 KB
testcase_01 AC 96 ms
71,860 KB
testcase_02 WA -
testcase_03 AC 595 ms
217,884 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
stdin=sys.stdin

ip=lambda: int(sp())
fp=lambda: float(sp())
lp=lambda:list(map(int,stdin.readline().split()))
sp=lambda:stdin.readline().rstrip()
yp=lambda:print('Yes')
np=lambda:print('No')

n=ip()
tonari=[[] for _ in range(n)]
st=[-1 for _ in range(n)]
for _ in range(n-1):
    a,b=lp()
    tonari[a-1].append(b-1)
    st[b-1]-=1

for i in range(n):
    if st[i]==-1:
        stx=i
        break
st[stx]=0
d={}
d[0]=1
q=deque([stx])
while q:
    now=q.popleft()
    for j in tonari[now]:
        if st[j]==-2:
            st[j]=st[now]+1
            if st[now]+1 not in d:
                d[st[now]+1]=0
            d[st[now]+1]+=1
            q.append(j)

ans=0
st.sort(reverse=True)
s=sum(st)
rest=n-1
for j in st:
    if d[j]!=0:
        ans+=s
        s-=rest
        rest-=d[j]
        d[j]=0
print(ans)
0