結果

問題 No.1103 Directed Length Sum
ユーザー qumazakiqumazaki
提出日時 2020-07-04 02:32:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,757 ms / 3,000 ms
コード長 788 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 81,704 KB
実行使用メモリ 358,448 KB
最終ジャッジ日時 2023-10-17 08:31:08
合計ジャッジ時間 16,985 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,388 KB
testcase_01 AC 35 ms
53,388 KB
testcase_02 AC 803 ms
347,248 KB
testcase_03 AC 571 ms
353,732 KB
testcase_04 AC 871 ms
265,032 KB
testcase_05 AC 1,757 ms
358,448 KB
testcase_06 AC 495 ms
196,348 KB
testcase_07 AC 133 ms
109,376 KB
testcase_08 AC 185 ms
126,488 KB
testcase_09 AC 103 ms
96,292 KB
testcase_10 AC 293 ms
137,692 KB
testcase_11 AC 1,019 ms
267,448 KB
testcase_12 AC 609 ms
196,736 KB
testcase_13 AC 299 ms
144,000 KB
testcase_14 AC 91 ms
92,236 KB
testcase_15 AC 462 ms
170,692 KB
testcase_16 AC 1,244 ms
280,840 KB
testcase_17 AC 1,309 ms
300,368 KB
testcase_18 AC 296 ms
137,920 KB
testcase_19 AC 1,046 ms
267,692 KB
testcase_20 AC 117 ms
101,772 KB
testcase_21 AC 200 ms
121,788 KB
testcase_22 AC 857 ms
245,572 KB
testcase_23 AC 477 ms
181,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
ab = list(map(int,read().split()))
mod = 10**9 + 7

it = iter(ab)
root = (n+1)*n//2
parent = [0] * (n+1)
links = [[] for _ in range(n+1)]
for a,b in zip(it,it):
    links[a].append(b)
    parent[b] = a
    root -= b

tp = [root]
stack = [root]
while(stack):
    next = []
    while(stack):
        i = stack.pop()
        for j in links[i]:
            next.append(j)
            tp.append(j)
    stack = next[::]

score = [[0,0] for _ in range(n+1)]

ans = 0
for i in tp[::-1]:
    ans += score[i][0]
    par = parent[i]
    score[par][0] += score[i][0] + score[i][1] + 1
    score[par][0] %= mod
    score[par][1] += score[i][1] + 1


print(ans%mod)
0