結果

問題 No.1103 Directed Length Sum
コンテスト
ユーザー marroncastle
提出日時 2020-07-25 19:25:53
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 942 ms / 3,000 ms
コード長 466 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 317 ms
コンパイル使用メモリ 85,568 KB
実行使用メモリ 254,360 KB
最終ジャッジ日時 2026-03-15 11:09:42
合計ジャッジ時間 9,963 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
import sys
input = sys.stdin.readline
N = int(input())
edge = [[] for _ in range(N)]
roots = [True]*N
for _ in range(N-1):
    a,b = map(int, input().split())
    roots[b-1] = False
    edge[a-1].append(b-1)

ans = 0
mod = 10**9+7
d = deque()
root = roots.index(True)
d.append([root,0])
while len(d)>0:
    v,depth = d.popleft()
    ans += depth*(depth+1)//2
    ans %= mod
    for w in edge[v]:
        d.append([w,depth+1])
print(ans)
0