結果

問題 No.1103 Directed Length Sum
ユーザー titia
提出日時 2020-07-03 21:51:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,765 ms / 3,000 ms
コード長 644 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 275,928 KB
最終ジャッジ日時 2024-09-17 00:19:20
合計ジャッジ時間 15,486 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

mod=10**9+7

D=[0]*(N+1)
D[0]=-1
P=[-1]*(N+1)

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

Parents=[1]*(N+1)
Children=[1]*(N+1)

R=D.index(0)

Q=[R]
TOP_SORT=[]
while Q:
    x=Q.pop()
    TOP_SORT.append(x)
    for to in E[x]:
        Parents[to]=Parents[x]+1
        Q.append(to)

for t in TOP_SORT[::-1][:-1]:
    Children[P[t]]+=Children[t]

ANS=0

for i in range(1,N+1):
    for to in E[i]:
        ANS=(ANS+Parents[i]*Children[to])%mod
        #print(i,to,Parents[i],Children[to])

print(ANS)


        
0