結果

問題 No.1103 Directed Length Sum
ユーザー ああいいああいい
提出日時 2022-03-08 12:36:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,690 ms / 3,000 ms
コード長 493 bytes
コンパイル時間 640 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 261,336 KB
最終ジャッジ日時 2023-09-30 18:17:00
合計ジャッジ時間 19,737 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,380 KB
testcase_01 AC 92 ms
71,092 KB
testcase_02 AC 837 ms
198,796 KB
testcase_03 AC 882 ms
261,336 KB
testcase_04 AC 987 ms
130,120 KB
testcase_05 AC 1,690 ms
166,356 KB
testcase_06 AC 659 ms
112,244 KB
testcase_07 AC 217 ms
85,636 KB
testcase_08 AC 281 ms
89,916 KB
testcase_09 AC 172 ms
81,936 KB
testcase_10 AC 374 ms
94,412 KB
testcase_11 AC 1,064 ms
134,964 KB
testcase_12 AC 668 ms
112,484 KB
testcase_13 AC 378 ms
94,956 KB
testcase_14 AC 158 ms
81,352 KB
testcase_15 AC 539 ms
105,252 KB
testcase_16 AC 1,201 ms
141,420 KB
testcase_17 AC 1,265 ms
144,084 KB
testcase_18 AC 354 ms
94,496 KB
testcase_19 AC 1,108 ms
136,076 KB
testcase_20 AC 188 ms
83,928 KB
testcase_21 AC 273 ms
88,244 KB
testcase_22 AC 911 ms
125,620 KB
testcase_23 AC 563 ms
106,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
G = [[] for _ in range(N+1)]
Gnum = [0] * (N + 1)
for _ in range(N-1):
    a,b = map(int,input().split())
    G[a].append(b)
    Gnum[b] += 1
root = -1
for i in range(1,N+1):
    if Gnum[i] == 0:
        root = i
        break
    

P = 10 ** 9 + 7
depth = [0] * (N + 1)
stack = [root]
ans = 0
while stack:
    now = stack.pop()
    for v in G[now]:
        depth[v] = depth[now] + 1
        ans = (ans + depth[v] * (depth[v] + 1) // 2) % P
        stack.append(v)
print(ans)
0