結果
| 問題 | No.1103 Directed Length Sum | 
| コンテスト | |
| ユーザー |  wolgnik | 
| 提出日時 | 2020-07-03 21:49:39 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,620 ms / 3,000 ms | 
| コード長 | 736 bytes | 
| コンパイル時間 | 207 ms | 
| コンパイル使用メモリ | 82,328 KB | 
| 実行使用メモリ | 277,300 KB | 
| 最終ジャッジ日時 | 2024-09-17 00:09:27 | 
| 合計ジャッジ時間 | 15,291 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge6 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 22 | 
ソースコード
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)
            
            
            
        