結果

問題 No.1103 Directed Length Sum
ユーザー lloyzlloyz
提出日時 2023-05-17 00:10:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,583 ms / 3,000 ms
コード長 632 bytes
コンパイル時間 608 ms
コンパイル使用メモリ 86,860 KB
実行使用メモリ 344,056 KB
最終ジャッジ日時 2023-08-21 11:05:30
合計ジャッジ時間 26,112 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,352 KB
testcase_01 AC 91 ms
71,400 KB
testcase_02 AC 1,322 ms
344,056 KB
testcase_03 AC 1,068 ms
332,408 KB
testcase_04 AC 1,484 ms
281,932 KB
testcase_05 AC 2,583 ms
307,480 KB
testcase_06 AC 960 ms
197,928 KB
testcase_07 AC 283 ms
98,236 KB
testcase_08 AC 387 ms
111,360 KB
testcase_09 AC 220 ms
90,644 KB
testcase_10 AC 516 ms
130,256 KB
testcase_11 AC 1,577 ms
310,828 KB
testcase_12 AC 938 ms
195,432 KB
testcase_13 AC 502 ms
135,944 KB
testcase_14 AC 198 ms
87,392 KB
testcase_15 AC 740 ms
161,516 KB
testcase_16 AC 1,822 ms
287,868 KB
testcase_17 AC 1,865 ms
286,184 KB
testcase_18 AC 512 ms
130,244 KB
testcase_19 AC 1,645 ms
324,804 KB
testcase_20 AC 258 ms
95,088 KB
testcase_21 AC 375 ms
108,600 KB
testcase_22 AC 1,316 ms
246,780 KB
testcase_23 AC 804 ms
175,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque, defaultdict

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)
0