結果

問題 No.1103 Directed Length Sum
ユーザー tktk_snsntktk_snsn
提出日時 2020-12-20 15:47:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,230 ms / 3,000 ms
コード長 563 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 81,684 KB
実行使用メモリ 247,972 KB
最終ジャッジ日時 2023-10-21 10:46:25
合計ジャッジ時間 14,272 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,444 KB
testcase_01 AC 37 ms
53,444 KB
testcase_02 AC 441 ms
199,096 KB
testcase_03 AC 405 ms
247,972 KB
testcase_04 AC 790 ms
127,432 KB
testcase_05 AC 1,230 ms
164,380 KB
testcase_06 AC 453 ms
109,736 KB
testcase_07 AC 121 ms
82,480 KB
testcase_08 AC 172 ms
87,956 KB
testcase_09 AC 97 ms
80,468 KB
testcase_10 AC 245 ms
92,864 KB
testcase_11 AC 770 ms
133,044 KB
testcase_12 AC 459 ms
110,108 KB
testcase_13 AC 227 ms
91,412 KB
testcase_14 AC 85 ms
79,476 KB
testcase_15 AC 358 ms
102,328 KB
testcase_16 AC 853 ms
139,952 KB
testcase_17 AC 909 ms
142,572 KB
testcase_18 AC 218 ms
91,028 KB
testcase_19 AC 801 ms
134,260 KB
testcase_20 AC 105 ms
81,208 KB
testcase_21 AC 164 ms
86,936 KB
testcase_22 AC 622 ms
123,532 KB
testcase_23 AC 366 ms
103,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
mod = 10 ** 9 + 7

N = int(input())
child = [[] for _ in range(N)]
deg = [0] * N
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    child[a].append(b)
    deg[b] += 1

for i, c in enumerate(deg):
    if not c:
        root = i
        break

dist = [0] * N
que = [root]
while que:
    s = que.pop()
    for t in child[s]:
        dist[t] = dist[s] + 1
        que.append(t)

ans = 0
for d in dist:
    ans += d * (d + 1) // 2
    ans %= mod

print(ans)
0