結果

問題 No.1103 Directed Length Sum
ユーザー Kiri8128Kiri8128
提出日時 2020-07-03 22:11:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,662 ms / 3,000 ms
コード長 690 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 81,804 KB
実行使用メモリ 302,108 KB
最終ジャッジ日時 2023-10-17 03:51:47
合計ジャッジ時間 21,826 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,616 KB
testcase_01 AC 40 ms
55,616 KB
testcase_02 AC 610 ms
261,652 KB
testcase_03 AC 723 ms
302,108 KB
testcase_04 AC 1,430 ms
197,640 KB
testcase_05 AC 2,662 ms
266,368 KB
testcase_06 AC 891 ms
149,896 KB
testcase_07 AC 218 ms
96,224 KB
testcase_08 AC 334 ms
107,364 KB
testcase_09 AC 153 ms
88,292 KB
testcase_10 AC 447 ms
112,508 KB
testcase_11 AC 1,596 ms
204,676 KB
testcase_12 AC 868 ms
147,540 KB
testcase_13 AC 441 ms
115,392 KB
testcase_14 AC 131 ms
84,424 KB
testcase_15 AC 677 ms
143,060 KB
testcase_16 AC 1,844 ms
216,964 KB
testcase_17 AC 1,943 ms
243,028 KB
testcase_18 AC 430 ms
113,828 KB
testcase_19 AC 1,583 ms
210,144 KB
testcase_20 AC 164 ms
90,588 KB
testcase_21 AC 283 ms
104,428 KB
testcase_22 AC 1,299 ms
181,240 KB
testcase_23 AC 753 ms
148,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda: sys.stdin.readline().rstrip()

from collections import deque
N = int(input())
X = [[] for i in range(N)]
Y = [0] * N
for i in range(N-1):
    x, y = map(int, input().split())
    X[x-1].append(y-1)
    X[y-1].append(x-1)
    Y[y-1] += 1

for i in range(N):
    if Y[i] == 0:
        s = i
        break

P = [-1] * N
Q = deque([s])

D = [0] * N
R = []
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a in X[i]:
        if a != P[i]:
            P[a] = i
            D[a] = D[i] + 1
            X[a].remove(i)
            deque.append(Q, a)

C = [1] * N
ans = 0
for i in R[1:][::-1]:
    C[P[i]] += C[i]
    ans += D[i] * C[i]

print(ans % (10 ** 9 + 7))
0