結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 21:43:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,440 ms / 3,000 ms
コード長 450 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 267,668 KB
最終ジャッジ日時 2024-04-17 01:46:33
合計ジャッジ時間 14,322 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,888 KB
testcase_01 AC 37 ms
54,124 KB
testcase_02 AC 677 ms
192,640 KB
testcase_03 AC 650 ms
267,668 KB
testcase_04 AC 823 ms
125,184 KB
testcase_05 AC 1,440 ms
158,236 KB
testcase_06 AC 516 ms
108,596 KB
testcase_07 AC 151 ms
83,556 KB
testcase_08 AC 198 ms
88,452 KB
testcase_09 AC 130 ms
81,152 KB
testcase_10 AC 295 ms
92,672 KB
testcase_11 AC 874 ms
129,884 KB
testcase_12 AC 518 ms
108,728 KB
testcase_13 AC 265 ms
93,020 KB
testcase_14 AC 106 ms
80,156 KB
testcase_15 AC 403 ms
101,248 KB
testcase_16 AC 977 ms
136,320 KB
testcase_17 AC 1,032 ms
138,240 KB
testcase_18 AC 250 ms
91,904 KB
testcase_19 AC 882 ms
130,688 KB
testcase_20 AC 134 ms
82,548 KB
testcase_21 AC 193 ms
86,660 KB
testcase_22 AC 743 ms
121,216 KB
testcase_23 AC 425 ms
103,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n=int(input())
mod=10**9+7
es=[[] for _ in range(n)]
par=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    es[a-1].append(b-1)
    par[b-1]=1

for i in range(n):
    if par[i]==0:
        root=i
        break
stack=[(root,0)]
ans=0

from collections import defaultdict
d=defaultdict(int)

while stack:
    now,depth=stack.pop()
    ans+=depth*(depth+1)//2
    ans%=mod
    for e in es[now]:
        stack.append((e, depth+1))
print(ans)
0