結果

問題 No.1103 Directed Length Sum
ユーザー titiatitia
提出日時 2020-07-03 21:51:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,405 ms / 3,000 ms
コード長 644 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 81,796 KB
実行使用メモリ 275,608 KB
最終ジャッジ日時 2023-10-17 01:19:58
合計ジャッジ時間 13,761 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,548 KB
testcase_01 AC 36 ms
53,548 KB
testcase_02 AC 561 ms
267,296 KB
testcase_03 AC 475 ms
275,608 KB
testcase_04 AC 813 ms
169,696 KB
testcase_05 AC 1,405 ms
247,168 KB
testcase_06 AC 511 ms
146,580 KB
testcase_07 AC 157 ms
91,848 KB
testcase_08 AC 199 ms
101,408 KB
testcase_09 AC 103 ms
84,828 KB
testcase_10 AC 248 ms
110,368 KB
testcase_11 AC 914 ms
187,472 KB
testcase_12 AC 520 ms
147,136 KB
testcase_13 AC 268 ms
112,344 KB
testcase_14 AC 93 ms
82,708 KB
testcase_15 AC 398 ms
124,264 KB
testcase_16 AC 1,039 ms
191,404 KB
testcase_17 AC 1,113 ms
217,756 KB
testcase_18 AC 250 ms
110,280 KB
testcase_19 AC 947 ms
200,624 KB
testcase_20 AC 121 ms
88,336 KB
testcase_21 AC 188 ms
99,108 KB
testcase_22 AC 776 ms
156,836 KB
testcase_23 AC 449 ms
136,532 KB
権限があれば一括ダウンロードができます

ソースコード

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