結果

問題 No.1103 Directed Length Sum
ユーザー wolgnikwolgnik
提出日時 2020-07-03 21:49:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,454 ms / 3,000 ms
コード長 736 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 276,248 KB
最終ジャッジ日時 2023-10-17 00:37:41
合計ジャッジ時間 13,629 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,552 KB
testcase_01 AC 34 ms
53,552 KB
testcase_02 AC 556 ms
264,028 KB
testcase_03 AC 472 ms
276,248 KB
testcase_04 AC 789 ms
169,580 KB
testcase_05 AC 1,454 ms
249,640 KB
testcase_06 AC 511 ms
146,572 KB
testcase_07 AC 140 ms
91,872 KB
testcase_08 AC 202 ms
101,428 KB
testcase_09 AC 110 ms
85,480 KB
testcase_10 AC 278 ms
110,376 KB
testcase_11 AC 891 ms
187,176 KB
testcase_12 AC 527 ms
147,132 KB
testcase_13 AC 264 ms
112,372 KB
testcase_14 AC 101 ms
82,744 KB
testcase_15 AC 413 ms
124,516 KB
testcase_16 AC 1,027 ms
192,772 KB
testcase_17 AC 1,079 ms
217,940 KB
testcase_18 AC 303 ms
110,304 KB
testcase_19 AC 917 ms
200,412 KB
testcase_20 AC 111 ms
88,240 KB
testcase_21 AC 172 ms
99,124 KB
testcase_22 AC 741 ms
160,720 KB
testcase_23 AC 436 ms
136,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N = int(input())
mod = 10 ** 9 + 7
e = [[] for _ in range(N + 1)]
inc = [0] * (N + 1)
for _ in range(N - 1):
  u, v = map(int, input().split())
  e[u].append(v)
  inc[v] += 1
root = 0
for x in range(1, N + 1):
  if inc[x] == 0:
    root = x
    break

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] % mod
    res %= mod
print(res)
0