結果

問題 No.1103 Directed Length Sum
ユーザー tikitaka1201tikitaka1201
提出日時 2020-07-09 21:57:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,375 ms / 3,000 ms
コード長 500 bytes
コンパイル時間 465 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 265,644 KB
最終ジャッジ日時 2024-04-17 03:14:17
合計ジャッジ時間 12,878 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
51,712 KB
testcase_01 AC 38 ms
52,096 KB
testcase_02 AC 458 ms
199,936 KB
testcase_03 AC 472 ms
265,644 KB
testcase_04 AC 703 ms
128,512 KB
testcase_05 AC 1,375 ms
164,992 KB
testcase_06 AC 429 ms
110,336 KB
testcase_07 AC 124 ms
83,820 KB
testcase_08 AC 163 ms
88,704 KB
testcase_09 AC 102 ms
80,640 KB
testcase_10 AC 214 ms
92,784 KB
testcase_11 AC 813 ms
133,760 KB
testcase_12 AC 435 ms
110,592 KB
testcase_13 AC 207 ms
93,312 KB
testcase_14 AC 92 ms
79,872 KB
testcase_15 AC 349 ms
103,680 KB
testcase_16 AC 914 ms
140,536 KB
testcase_17 AC 990 ms
143,104 KB
testcase_18 AC 225 ms
92,892 KB
testcase_19 AC 835 ms
135,156 KB
testcase_20 AC 110 ms
82,432 KB
testcase_21 AC 158 ms
87,552 KB
testcase_22 AC 650 ms
124,288 KB
testcase_23 AC 360 ms
104,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
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
    if visited[now]==0:
        visited[now]=1
        for e in es[now]:
            stack.append((e, depth+1))
print(ans)
0