結果

問題 No.1103 Directed Length Sum
ユーザー takakintakakin
提出日時 2020-07-31 00:25:24
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 654 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 10,940 KB
実行使用メモリ 373,492 KB
最終ジャッジ日時 2023-09-18 09:04:27
合計ジャッジ時間 5,793 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
12,212 KB
testcase_01 AC 15 ms
7,704 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
mod=10**9+7
edge=[[] for i in range(n)]
edge_r=[[] for i in range(n)]
A=[True]*n
for i in range(n-1):
	a,b=map(int,input().split())
	edge[a-1].append(b-1)
	edge_r[b-1].append(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]:
	s=TS[i]
	for g in edge_r[s]:
		C[g]+=C[s]+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:
		s,g=edge_r[i][0],i
		ans=(ans+(P[s]+1)*(C[g]+1))%mod
print(ans)
0