結果

問題 No.1103 Directed Length Sum
ユーザー lloyzlloyz
提出日時 2023-05-17 00:11:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,710 ms / 3,000 ms
コード長 788 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 344,388 KB
最終ジャッジ日時 2023-08-21 11:07:17
合計ジャッジ時間 27,193 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,640 KB
testcase_01 AC 95 ms
71,460 KB
testcase_02 AC 1,319 ms
344,388 KB
testcase_03 AC 1,137 ms
331,496 KB
testcase_04 AC 1,565 ms
281,860 KB
testcase_05 AC 2,710 ms
307,684 KB
testcase_06 AC 974 ms
198,076 KB
testcase_07 AC 306 ms
101,196 KB
testcase_08 AC 423 ms
111,816 KB
testcase_09 AC 240 ms
90,860 KB
testcase_10 AC 555 ms
130,588 KB
testcase_11 AC 1,689 ms
310,784 KB
testcase_12 AC 986 ms
195,288 KB
testcase_13 AC 545 ms
135,760 KB
testcase_14 AC 206 ms
87,716 KB
testcase_15 AC 764 ms
161,304 KB
testcase_16 AC 1,866 ms
287,908 KB
testcase_17 AC 2,050 ms
286,744 KB
testcase_18 AC 548 ms
130,216 KB
testcase_19 AC 1,725 ms
324,976 KB
testcase_20 AC 259 ms
95,136 KB
testcase_21 AC 381 ms
108,788 KB
testcase_22 AC 1,329 ms
247,396 KB
testcase_23 AC 833 ms
175,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque, defaultdict

def main():
    n = int(input())
    INcnt = defaultdict(int)
    OUTs = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        INcnt[b] += 1
        OUTs[a].append(b)

    C = [0 for _ in range(n)]
    Que = deque([i for i in range(n) if INcnt[i] == 0])
    while len(Que) != 0:
        v = Que.popleft()
        for v2 in OUTs[v]:
            C[v2] = C[v] + 1
            INcnt[v2] -= 1
            if INcnt[v2] == 0:
                Que.append(v2)
    mod = 10**9 + 7
    inv2 = pow(2, mod - 2, mod)
    ans = 0
    for i in range(n):
        c = C[i]
        ans += c * (c + 1) % mod * inv2 % mod
        ans %= mod
    print(ans)

if __name__ == '__main__':
    main()

0