結果

問題 No.1103 Directed Length Sum
ユーザー 双六双六
提出日時 2020-07-20 03:15:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,800 ms / 3,000 ms
コード長 1,116 bytes
コンパイル時間 959 ms
コンパイル使用メモリ 86,576 KB
実行使用メモリ 312,348 KB
最終ジャッジ日時 2023-08-23 07:36:16
合計ジャッジ時間 19,018 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
71,412 KB
testcase_01 AC 90 ms
71,688 KB
testcase_02 AC 927 ms
286,712 KB
testcase_03 AC 675 ms
290,028 KB
testcase_04 AC 990 ms
186,756 KB
testcase_05 AC 1,800 ms
312,348 KB
testcase_06 AC 635 ms
148,276 KB
testcase_07 AC 218 ms
95,276 KB
testcase_08 AC 297 ms
103,764 KB
testcase_09 AC 179 ms
88,504 KB
testcase_10 AC 362 ms
108,564 KB
testcase_11 AC 1,050 ms
207,056 KB
testcase_12 AC 629 ms
151,916 KB
testcase_13 AC 356 ms
114,008 KB
testcase_14 AC 159 ms
85,520 KB
testcase_15 AC 503 ms
131,004 KB
testcase_16 AC 1,212 ms
222,452 KB
testcase_17 AC 1,266 ms
240,868 KB
testcase_18 AC 355 ms
108,500 KB
testcase_19 AC 1,068 ms
212,496 KB
testcase_20 AC 190 ms
91,376 KB
testcase_21 AC 263 ms
101,144 KB
testcase_22 AC 861 ms
177,096 KB
testcase_23 AC 647 ms
136,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
from collections import deque
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

class Graph(object):
	def __init__(self):
		self.graph = defaultdict(list)

	def __len__(self):
		return len(self.graph)

	def add_edge(self, a, b):
		self.graph[a].append(b)

class BFS(object):
	def __init__(self, graph, s, N):
		self.g = graph.graph
		self.Q = deque(); self.Q.append(s)
		self.dist = [INF] * N; self.dist[s] = 0
		while self.Q:
			v = self.Q.popleft()
			for i in self.g[v]:
				if self.dist[i] == INF:
					self.dist[i] = self.dist[v] + 1
					self.Q.append(i)

#処理内容
def main():
	N = int(input())
	Ein = [0] * N
	G = Graph()
	for i in range(N - 1):
		a, b = getlist()
		a -= 1; b -= 1
		G.add_edge(a, b)
		Ein[b] = 1

	s = None
	for i in range(N):
		if Ein[i] == 0:
			s = i
			break

	BF = BFS(G, s, N)
	dist = BF.dist
	ans = 0
	for i in range(N):
		v = dist[i]
		ans += int((v * (v + 1)) // 2)

	print(ans % con)


if __name__ == '__main__':
	main()
0