結果

問題 No.1103 Directed Length Sum
ユーザー takakintakakin
提出日時 2020-07-31 00:42:20
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 2,351 ms / 3,000 ms
コード長 608 bytes
コンパイル時間 1,377 ms
コンパイル使用メモリ 83,904 KB
実行使用メモリ 261,888 KB
最終ジャッジ日時 2023-09-18 09:41:00
合計ジャッジ時間 24,552 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,008 KB
testcase_01 AC 78 ms
71,228 KB
testcase_02 AC 917 ms
255,612 KB
testcase_03 AC 774 ms
261,888 KB
testcase_04 AC 1,266 ms
173,816 KB
testcase_05 AC 2,351 ms
248,648 KB
testcase_06 AC 830 ms
142,836 KB
testcase_07 AC 226 ms
92,408 KB
testcase_08 AC 305 ms
100,972 KB
testcase_09 AC 174 ms
86,216 KB
testcase_10 AC 407 ms
109,104 KB
testcase_11 AC 1,391 ms
186,252 KB
testcase_12 AC 780 ms
143,060 KB
testcase_13 AC 400 ms
111,368 KB
testcase_14 AC 153 ms
83,596 KB
testcase_15 AC 627 ms
128,196 KB
testcase_16 AC 1,577 ms
188,084 KB
testcase_17 AC 1,665 ms
208,376 KB
testcase_18 AC 390 ms
108,880 KB
testcase_19 AC 1,412 ms
177,176 KB
testcase_20 AC 196 ms
88,204 KB
testcase_21 AC 289 ms
98,616 KB
testcase_22 AC 1,150 ms
154,176 KB
testcase_23 AC 657 ms
133,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
mod=10**9+7
edge=[[] for i in range(n)]
Par=[0]*n
A=[True]*n
for i in range(n-1):
	a,b=map(int,input().split())
	edge[a-1].append(b-1)
	Par[b-1]=a-1
	A[b-1]=False

for i in range(n):
	if A[i]:
		root=i
		break

TS=[root]
S=[root]
while S:
	s=S.pop()
	for g in edge[s]:
		TS.append(g)
		S.append(g)

ans=0
C=[0]*n
for i in range(n)[:1:-1]:
	C[Par[TS[i]]]+=C[TS[i]]+1

P=[0]*n
for i in range(n):
	s=TS[i]
	for g in edge[s]:
		P[g]+=P[s]+1

for i in range(n):
	if i!=root:
		ans+=(P[Par[i]]+1)*(C[i]+1)
		if ans>=mod:
			ans%=mod
print(ans)
0