結果

問題 No.1103 Directed Length Sum
ユーザー tonnnura172tonnnura172
提出日時 2020-07-11 05:02:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,177 ms / 3,000 ms
コード長 1,538 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 275,632 KB
最終ジャッジ日時 2024-04-20 10:59:19
合計ジャッジ時間 22,245 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
65,536 KB
testcase_01 AC 72 ms
65,664 KB
testcase_02 AC 765 ms
263,424 KB
testcase_03 AC 698 ms
275,632 KB
testcase_04 AC 1,202 ms
166,248 KB
testcase_05 AC 2,177 ms
243,820 KB
testcase_06 AC 753 ms
137,124 KB
testcase_07 AC 231 ms
94,464 KB
testcase_08 AC 312 ms
103,552 KB
testcase_09 AC 173 ms
87,424 KB
testcase_10 AC 414 ms
112,768 KB
testcase_11 AC 1,278 ms
186,112 KB
testcase_12 AC 756 ms
137,732 KB
testcase_13 AC 439 ms
114,304 KB
testcase_14 AC 163 ms
85,376 KB
testcase_15 AC 623 ms
128,232 KB
testcase_16 AC 1,485 ms
192,152 KB
testcase_17 AC 1,643 ms
194,560 KB
testcase_18 AC 408 ms
112,268 KB
testcase_19 AC 1,345 ms
195,972 KB
testcase_20 AC 189 ms
89,824 KB
testcase_21 AC 286 ms
98,304 KB
testcase_22 AC 1,115 ms
173,668 KB
testcase_23 AC 741 ms
137,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd, log2
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul, add
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
tree = [[] for _ in range(N)]
din = [0]*N
for _ in range(N-1):
    A, B = MAP()
    tree[A-1].append(B-1)
    din[B-1] += 1

root = din.index(0)

size_d = [0]*N
size_u = [0]*N
parent = [0]*N
order = []
stack = [root]
while stack:
    x = stack.pop()
    size_u[x] += 1
    order.append(x)
    for y in tree[x]:
        size_u[y] += size_u[x]
        parent[y] = x
        stack.append(y)

for v in order[::-1]:  # 根に遠いほうから(down方向のボトムアップ)
    size_d[v] += 1
    if v == root:
        break
    p = parent[v]
    s = size_d[v]
    size_d[p] += s
# print(size_u, size_d)
q = deque([root])
ans = 0
while q:
    n = q.popleft()
    for node in tree[n]:
        ans += size_u[n]*size_d[node]
        ans %= mod
        q.append(node)
print(ans)
0