結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 20:58:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,779 ms / 3,000 ms
コード長 490 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 214,840 KB
最終ジャッジ日時 2024-04-16 23:55:45
合計ジャッジ時間 18,546 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,760 KB
testcase_01 AC 47 ms
53,760 KB
testcase_02 AC 822 ms
192,896 KB
testcase_03 AC 716 ms
214,840 KB
testcase_04 AC 1,028 ms
125,184 KB
testcase_05 AC 1,779 ms
158,592 KB
testcase_06 AC 682 ms
109,056 KB
testcase_07 AC 214 ms
84,096 KB
testcase_08 AC 282 ms
88,960 KB
testcase_09 AC 163 ms
81,152 KB
testcase_10 AC 367 ms
92,672 KB
testcase_11 AC 1,079 ms
129,664 KB
testcase_12 AC 670 ms
108,928 KB
testcase_13 AC 376 ms
93,440 KB
testcase_14 AC 146 ms
80,128 KB
testcase_15 AC 634 ms
101,760 KB
testcase_16 AC 1,324 ms
136,320 KB
testcase_17 AC 1,303 ms
138,496 KB
testcase_18 AC 359 ms
92,544 KB
testcase_19 AC 1,140 ms
130,944 KB
testcase_20 AC 183 ms
82,944 KB
testcase_21 AC 266 ms
87,552 KB
testcase_22 AC 929 ms
120,960 KB
testcase_23 AC 599 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, Counter
stack=deque([(root,0)])
ans=0
while stack:
    now,depth=stack.pop()
    for e in es[now]:
        stack.append((e, depth+1))
        ans+=(depth+1)*(depth+2)//2
        ans%=mod
print(ans%mod)
0