結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 21:28:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,630 ms / 3,000 ms
コード長 470 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 82,816 KB
実行使用メモリ 215,388 KB
最終ジャッジ日時 2024-04-17 01:17:59
合計ジャッジ時間 16,282 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
53,760 KB
testcase_01 AC 42 ms
53,760 KB
testcase_02 AC 773 ms
192,640 KB
testcase_03 AC 658 ms
215,388 KB
testcase_04 AC 943 ms
125,260 KB
testcase_05 AC 1,630 ms
158,336 KB
testcase_06 AC 618 ms
108,640 KB
testcase_07 AC 192 ms
83,840 KB
testcase_08 AC 265 ms
88,808 KB
testcase_09 AC 154 ms
81,096 KB
testcase_10 AC 350 ms
92,544 KB
testcase_11 AC 1,011 ms
129,792 KB
testcase_12 AC 640 ms
109,056 KB
testcase_13 AC 357 ms
93,104 KB
testcase_14 AC 135 ms
80,000 KB
testcase_15 AC 520 ms
101,756 KB
testcase_16 AC 1,166 ms
135,808 KB
testcase_17 AC 1,173 ms
138,496 KB
testcase_18 AC 336 ms
92,800 KB
testcase_19 AC 1,035 ms
130,876 KB
testcase_20 AC 168 ms
82,816 KB
testcase_21 AC 247 ms
87,444 KB
testcase_22 AC 852 ms
121,044 KB
testcase_23 AC 541 ms
103,808 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
# dfs
from collections import deque
stack=deque([(root,0)])
ans=0

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%mod)
0