結果

問題 No.1103 Directed Length Sum
ユーザー toyuzukotoyuzuko
提出日時 2020-07-07 15:38:13
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 854 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 347,964 KB
最終ジャッジ日時 2024-04-08 15:58:43
合計ジャッジ時間 25,125 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 1,405 ms
335,904 KB
testcase_03 AC 1,384 ms
347,964 KB
testcase_04 AC 1,570 ms
205,756 KB
testcase_05 TLE -
testcase_06 AC 981 ms
155,672 KB
testcase_07 AC 236 ms
94,244 KB
testcase_08 AC 341 ms
105,612 KB
testcase_09 AC 176 ms
87,588 KB
testcase_10 AC 492 ms
116,624 KB
testcase_11 AC 1,695 ms
216,944 KB
testcase_12 AC 954 ms
156,448 KB
testcase_13 AC 473 ms
115,644 KB
testcase_14 AC 152 ms
82,488 KB
testcase_15 AC 779 ms
143,572 KB
testcase_16 AC 1,947 ms
234,816 KB
testcase_17 AC 2,050 ms
244,548 KB
testcase_18 AC 463 ms
116,620 KB
testcase_19 AC 1,793 ms
220,928 KB
testcase_20 AC 203 ms
90,880 KB
testcase_21 AC 320 ms
101,164 KB
testcase_22 AC 1,435 ms
194,352 KB
testcase_23 AC 788 ms
147,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 1000000007

N = int(input())
edge = [tuple(map(int, input().split())) for _ in range(N - 1)]
tree = [[] for _ in range(N)]
par = [None for _ in range(N)]
dep = [None for _ in range(N)]
cum = [None for _ in range(N)]

for e in edge:
    tree[e[0] - 1].append(e[1] - 1)
    par[e[1] - 1] = e[0] - 1

for i in range(N):
    if par[i] is None:
        root = i

stack = [root]
dep[root] = 0

while stack:
    node = stack.pop()
    for adj in tree[node]:
        if dep[adj] is not None:
            continue
        dep[adj] = dep[node] + 1
        dep[adj] %= MOD
        stack.append(adj)

stack = [root]
cum[root] = 0

while stack:
    node = stack.pop()
    for adj in tree[node]:
        if cum[adj] is not None:
            continue
        cum[adj] = cum[node] + dep[adj]
        cum[adj] %= MOD
        stack.append(adj)

print(sum(cum) % MOD)
0