結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,616 KB
testcase_01 AC 41 ms
55,616 KB
testcase_02 AC 606 ms
261,648 KB
testcase_03 AC 718 ms
302,108 KB
testcase_04 AC 1,449 ms
197,632 KB
testcase_05 AC 2,684 ms
266,368 KB
testcase_06 AC 889 ms
149,896 KB
testcase_07 AC 213 ms
96,224 KB
testcase_08 AC 321 ms
107,368 KB
testcase_09 AC 148 ms
88,292 KB
testcase_10 AC 441 ms
112,512 KB
testcase_11 AC 1,563 ms
204,672 KB
testcase_12 AC 884 ms
147,540 KB
testcase_13 AC 439 ms
115,392 KB
testcase_14 AC 129 ms
84,424 KB
testcase_15 AC 690 ms
143,060 KB
testcase_16 AC 1,814 ms
216,368 KB
testcase_17 AC 1,925 ms
243,032 KB
testcase_18 AC 438 ms
113,828 KB
testcase_19 AC 1,589 ms
210,124 KB
testcase_20 AC 171 ms
90,592 KB
testcase_21 AC 291 ms
104,428 KB
testcase_22 AC 1,316 ms
181,248 KB
testcase_23 AC 759 ms
148,744 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