結果

問題 No.1103 Directed Length Sum
ユーザー terasaterasa
提出日時 2022-06-09 01:43:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,965 ms / 3,000 ms
コード長 1,557 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 81,764 KB
実行使用メモリ 292,092 KB
最終ジャッジ日時 2023-10-21 04:19:27
合計ジャッジ時間 23,926 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,684 KB
testcase_01 AC 46 ms
55,684 KB
testcase_02 AC 1,008 ms
292,092 KB
testcase_03 AC 815 ms
272,452 KB
testcase_04 AC 1,514 ms
192,696 KB
testcase_05 AC 2,965 ms
273,172 KB
testcase_06 AC 995 ms
150,524 KB
testcase_07 AC 249 ms
92,124 KB
testcase_08 AC 369 ms
102,760 KB
testcase_09 AC 184 ms
86,700 KB
testcase_10 AC 500 ms
112,636 KB
testcase_11 AC 1,705 ms
203,176 KB
testcase_12 AC 995 ms
151,516 KB
testcase_13 AC 505 ms
113,476 KB
testcase_14 AC 159 ms
83,360 KB
testcase_15 AC 763 ms
135,196 KB
testcase_16 AC 1,932 ms
217,192 KB
testcase_17 AC 2,020 ms
221,144 KB
testcase_18 AC 493 ms
112,740 KB
testcase_19 AC 1,687 ms
203,600 KB
testcase_20 AC 211 ms
88,484 KB
testcase_21 AC 333 ms
99,400 KB
testcase_22 AC 1,419 ms
181,784 KB
testcase_23 AC 840 ms
137,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


N = int(input())
mod = 10 ** 9 + 7
E = [[] for _ in range(N)]
par = [-1] * N
D = [0] * N
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    E[a].append(b)
    D[b] += 1
    par[b] = a

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

dq = deque([s])
INF = 1 << 30
depth = [INF] * N
depth[s] = 0
while dq:
    v = dq.popleft()
    for d in E[v]:
        if depth[d] > depth[v] + 1:
            depth[d] = depth[v] + 1
            dq.append(d)

V = [(i, depth[i]) for i in range(N)]
V.sort(key=lambda x: x[1], reverse=True)

cnt = [1] * N
acc = [0] * N
for i, _ in V:
    acc[i] += cnt[i] - 1
    if i == s:
        break
    p = par[i]
    cnt[p] += cnt[i]
    acc[p] += acc[i]
ans = 0
for i in range(N):
    ans += acc[i]
    ans %= mod
print(ans)
0