結果

問題 No.1103 Directed Length Sum
ユーザー wolgnikwolgnik
提出日時 2020-07-03 21:46:50
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 852 bytes
コンパイル時間 146 ms
コンパイル使用メモリ 81,796 KB
実行使用メモリ 303,844 KB
最終ジャッジ日時 2023-10-17 00:28:28
合計ジャッジ時間 7,242 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,548 KB
testcase_01 AC 34 ms
53,544 KB
testcase_02 AC 559 ms
263,164 KB
testcase_03 AC 546 ms
291,012 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N = int(input())
mod = 10 ** 9 + 7
e = [[] for _ in range(N + 1)]
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
root = 0
vis = [0] * (N + 1)
for x in range(1, N + 1):
  if vis[x]: continue
  root = x
  vis = [0] * (N + 1)
  s = [x]
  while len(s):
    p = s.pop()
    for q in e[p]:
      if vis[q]: continue
      vis[q] = 1
      s.append(q)

vis = [0] * (N + 1)
size = [1] * (N + 1)
parent = [0] * (N + 1)
depth = [1] * (N + 1)
s = [root]
order = []
while len(s):
  x = s.pop()
  order.append(x)
  for y in e[x]:
    if vis[y]: continue
    parent[y] = x
    depth[y] = depth[x] + 1
    s.append(y)

order.reverse()
for y in order[: -1]:
  x = parent[y]
  size[x] += size[y]

res = 0
for x in range(1, N + 1):
  for y in e[x]:
    res += depth[x] * size[y]
    res %= mod
print(res)
0