結果

問題 No.1103 Directed Length Sum
ユーザー anagohirameanagohirame
提出日時 2020-07-03 23:49:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,572 ms / 3,000 ms
コード長 538 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 81,780 KB
実行使用メモリ 262,384 KB
最終ジャッジ日時 2023-10-17 06:30:00
合計ジャッジ時間 13,784 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,456 KB
testcase_01 AC 38 ms
53,456 KB
testcase_02 AC 442 ms
195,840 KB
testcase_03 AC 357 ms
262,384 KB
testcase_04 AC 698 ms
128,588 KB
testcase_05 AC 1,227 ms
164,456 KB
testcase_06 AC 448 ms
109,880 KB
testcase_07 AC 122 ms
82,364 KB
testcase_08 AC 177 ms
87,480 KB
testcase_09 AC 101 ms
80,420 KB
testcase_10 AC 243 ms
92,680 KB
testcase_11 AC 762 ms
133,056 KB
testcase_12 AC 458 ms
110,364 KB
testcase_13 AC 240 ms
93,004 KB
testcase_14 AC 91 ms
79,556 KB
testcase_15 AC 359 ms
102,756 KB
testcase_16 AC 1,297 ms
140,040 KB
testcase_17 AC 1,572 ms
142,652 KB
testcase_18 AC 505 ms
92,600 KB
testcase_19 AC 960 ms
134,524 KB
testcase_20 AC 111 ms
81,144 KB
testcase_21 AC 172 ms
86,696 KB
testcase_22 AC 635 ms
123,596 KB
testcase_23 AC 380 ms
104,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(1000010)
def input():
	return sys.stdin.buffer.readline()[:-1]
MOD = 10**9+7

n = int(input())
aff = [0 for _ in range(n)]
adj = [[] for _ in range(n)]
for _ in range(n-1):
	u, v = map(int, input().split())
	adj[u-1].append(v-1)
	aff[v-1] += 1
for i, x in enumerate(aff):
	if x == 0:
		root = i
		break

ans = 0
dep = [0 for _ in range(n)]
ans = 0
stack = [root]
while stack:
	i = stack.pop()
	ans += dep[i] * (dep[i]+1) // 2
	ans %= MOD
	for j in adj[i]:
		dep[j] = dep[i]+1
		stack.append(j)

print(ans)
0