結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 21:35:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,089 ms / 3,000 ms
コード長 513 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 283,304 KB
最終ジャッジ日時 2024-04-17 01:30:30
合計ジャッジ時間 20,751 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
53,504 KB
testcase_01 AC 46 ms
53,376 KB
testcase_02 AC 1,165 ms
283,304 KB
testcase_03 AC 824 ms
278,888 KB
testcase_04 AC 1,175 ms
125,184 KB
testcase_05 AC 2,089 ms
158,720 KB
testcase_06 AC 758 ms
108,928 KB
testcase_07 AC 220 ms
83,968 KB
testcase_08 AC 298 ms
88,704 KB
testcase_09 AC 169 ms
81,024 KB
testcase_10 AC 399 ms
92,800 KB
testcase_11 AC 1,286 ms
129,792 KB
testcase_12 AC 738 ms
109,184 KB
testcase_13 AC 401 ms
93,056 KB
testcase_14 AC 154 ms
80,384 KB
testcase_15 AC 592 ms
101,888 KB
testcase_16 AC 1,447 ms
136,320 KB
testcase_17 AC 1,758 ms
138,368 KB
testcase_18 AC 440 ms
92,672 KB
testcase_19 AC 1,344 ms
131,072 KB
testcase_20 AC 191 ms
82,816 KB
testcase_21 AC 289 ms
87,552 KB
testcase_22 AC 1,054 ms
121,216 KB
testcase_23 AC 603 ms
103,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

from collections import defaultdict
d=defaultdict(int)

while stack:
    now,depth=stack.pop()
    d[depth]+=1
    for e in es[now]:
        stack.append((e, depth+1))

for k in d.keys():
    ans+=d[k]*k*(k+1)//2
    ans%=mod

print(ans)
0