結果

問題 No.1103 Directed Length Sum
ユーザー asumo0729asumo0729
提出日時 2020-10-17 18:35:37
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 839 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 86,624 KB
実行使用メモリ 836,256 KB
最終ジャッジ日時 2023-09-28 08:28:30
合計ジャッジ時間 5,139 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys
sys.setrecursionlimit(1000000)
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=[0 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]==0:
        stx=i
        break
st[stx]=-1
d={}
d[0]=1

def dfs(now,ch):
    for j in tonari[now]:
        if st[j]!=-1:
            st[j]=ch+1
            if ch+1 not in d:
                d[ch+1]=0
            d[ch+1]+=1
                
            dfs(j,ch+1)


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