結果

問題 No.1103 Directed Length Sum
ユーザー wolgnik
提出日時 2020-07-03 21:46:50
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 852 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 307,060 KB
最終ジャッジ日時 2024-09-17 00:01:38
合計ジャッジ時間 6,762 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 2 TLE * 1 -- * 19
権限があれば一括ダウンロードができます

ソースコード

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