結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 20:56:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,593 ms / 3,000 ms
コード長 526 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 276,284 KB
最終ジャッジ日時 2024-04-16 23:52:28
合計ジャッジ時間 17,116 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,016 KB
testcase_01 AC 39 ms
53,760 KB
testcase_02 AC 809 ms
245,928 KB
testcase_03 AC 693 ms
276,284 KB
testcase_04 AC 928 ms
157,880 KB
testcase_05 AC 1,593 ms
207,316 KB
testcase_06 AC 567 ms
130,096 KB
testcase_07 AC 170 ms
89,856 KB
testcase_08 AC 219 ms
98,304 KB
testcase_09 AC 130 ms
84,052 KB
testcase_10 AC 293 ms
104,436 KB
testcase_11 AC 969 ms
164,684 KB
testcase_12 AC 598 ms
129,560 KB
testcase_13 AC 317 ms
107,364 KB
testcase_14 AC 121 ms
82,432 KB
testcase_15 AC 497 ms
128,000 KB
testcase_16 AC 1,152 ms
174,280 KB
testcase_17 AC 1,171 ms
176,484 KB
testcase_18 AC 310 ms
103,456 KB
testcase_19 AC 1,029 ms
167,244 KB
testcase_20 AC 149 ms
86,768 KB
testcase_21 AC 225 ms
95,104 KB
testcase_22 AC 840 ms
148,956 KB
testcase_23 AC 481 ms
123,676 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

new=[]
# dfs
from collections import deque, Counter
stack=deque([(root,0)])
ans=0
while stack:
    now,depth=stack.pop()
    new.append((now,depth))
    for e in es[now]:
        stack.append((e, depth+1))
        ans+=(depth+1)*(depth+2)//2
        ans%=mod
print(ans%mod)
0