結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,684 KB
testcase_01 AC 90 ms
71,588 KB
testcase_02 AC 1,021 ms
350,212 KB
testcase_03 AC 840 ms
336,844 KB
testcase_04 AC 1,319 ms
269,064 KB
testcase_05 AC 2,295 ms
379,788 KB
testcase_06 AC 775 ms
190,024 KB
testcase_07 AC 248 ms
97,432 KB
testcase_08 AC 350 ms
110,692 KB
testcase_09 AC 194 ms
94,424 KB
testcase_10 AC 443 ms
123,220 KB
testcase_11 AC 1,407 ms
299,412 KB
testcase_12 AC 809 ms
189,864 KB
testcase_13 AC 454 ms
132,656 KB
testcase_14 AC 173 ms
87,384 KB
testcase_15 AC 644 ms
153,604 KB
testcase_16 AC 1,611 ms
317,232 KB
testcase_17 AC 1,654 ms
286,488 KB
testcase_18 AC 447 ms
125,924 KB
testcase_19 AC 1,446 ms
303,680 KB
testcase_20 AC 222 ms
94,660 KB
testcase_21 AC 329 ms
106,308 KB
testcase_22 AC 1,190 ms
242,500 KB
testcase_23 AC 748 ms
171,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque, defaultdict
import sys
input = sys.stdin.readline

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