結果

問題 No.1103 Directed Length Sum
ユーザー neterukunneterukun
提出日時 2020-07-03 22:49:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,138 ms / 3,000 ms
コード長 736 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 81,616 KB
実行使用メモリ 395,512 KB
最終ジャッジ日時 2023-10-17 05:51:59
合計ジャッジ時間 16,642 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,388 KB
testcase_01 AC 38 ms
53,388 KB
testcase_02 AC 850 ms
312,444 KB
testcase_03 AC 875 ms
395,512 KB
testcase_04 AC 996 ms
223,940 KB
testcase_05 AC 2,138 ms
348,308 KB
testcase_06 AC 553 ms
159,072 KB
testcase_07 AC 179 ms
98,536 KB
testcase_08 AC 204 ms
98,108 KB
testcase_09 AC 113 ms
83,236 KB
testcase_10 AC 279 ms
112,508 KB
testcase_11 AC 1,059 ms
237,024 KB
testcase_12 AC 626 ms
174,648 KB
testcase_13 AC 324 ms
126,432 KB
testcase_14 AC 105 ms
84,388 KB
testcase_15 AC 429 ms
139,316 KB
testcase_16 AC 1,209 ms
257,732 KB
testcase_17 AC 1,273 ms
262,004 KB
testcase_18 AC 287 ms
113,316 KB
testcase_19 AC 1,213 ms
252,044 KB
testcase_20 AC 144 ms
91,712 KB
testcase_21 AC 212 ms
103,968 KB
testcase_22 AC 902 ms
206,244 KB
testcase_23 AC 465 ms
149,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline


n = int(input())
info = [list(map(int, input().split())) for i in range(n - 1)]
MOD = 10 ** 9 + 7


tree = [[] for i in range(n)]
for a, b in info:
    a -= 1
    b -= 1
    tree[b].append(a)

cnt_ = [0] * n
for v in range(n):
    for nxt_v in tree[v]:
        cnt_[nxt_v] += 1

dp_cnt = [1] * n
dp_ptn = [0] * n
stack = [i for i in range(n) if cnt_[i] == 0]
while stack:
    v = stack.pop()
    for nxt_v in tree[v]:
        dp_cnt[nxt_v] += dp_cnt[v]
        dp_ptn[nxt_v] += dp_ptn[v] + dp_cnt[v]
        dp_ptn[nxt_v] %= MOD
        cnt_[nxt_v] -= 1
        if cnt_[nxt_v] == 0:
            stack.append(nxt_v)

ans = 0
for i in range(n):
    ans += dp_ptn[i]
    ans %= MOD
print(ans)
0