結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 21:47:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,368 ms / 3,000 ms
コード長 471 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 274,852 KB
最終ジャッジ日時 2024-04-17 01:53:52
合計ジャッジ時間 22,637 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
51,968 KB
testcase_01 AC 45 ms
51,712 KB
testcase_02 AC 936 ms
200,192 KB
testcase_03 AC 897 ms
274,852 KB
testcase_04 AC 1,346 ms
129,152 KB
testcase_05 AC 2,368 ms
165,248 KB
testcase_06 AC 867 ms
110,848 KB
testcase_07 AC 240 ms
83,968 KB
testcase_08 AC 334 ms
88,832 KB
testcase_09 AC 182 ms
81,024 KB
testcase_10 AC 465 ms
92,928 KB
testcase_11 AC 1,461 ms
134,016 KB
testcase_12 AC 878 ms
111,360 KB
testcase_13 AC 465 ms
94,336 KB
testcase_14 AC 161 ms
80,256 KB
testcase_15 AC 683 ms
103,040 KB
testcase_16 AC 1,618 ms
140,800 KB
testcase_17 AC 1,729 ms
143,488 KB
testcase_18 AC 443 ms
93,184 KB
testcase_19 AC 1,487 ms
135,296 KB
testcase_20 AC 211 ms
82,688 KB
testcase_21 AC 309 ms
87,168 KB
testcase_22 AC 1,183 ms
124,672 KB
testcase_23 AC 708 ms
105,472 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
visited=[0]*n
while stack:
    now,depth=stack.pop()
    ans+=depth*(depth+1)//2
    ans%=mod
    x=es[now]
    for e in x:
        if visited[e]==1: continue
        stack.append((e, depth+1))
        visited[e]=1
print(ans)
0