結果

問題 No.1207 グラフX
ユーザー 双六双六
提出日時 2020-09-01 00:07:21
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,112 ms / 2,000 ms
コード長 2,232 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 86,756 KB
実行使用メモリ 258,188 KB
最終ジャッジ日時 2023-08-10 18:15:00
合計ジャッジ時間 33,451 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 681 ms
231,968 KB
testcase_01 AC 675 ms
231,904 KB
testcase_02 AC 693 ms
228,948 KB
testcase_03 AC 702 ms
231,896 KB
testcase_04 AC 691 ms
231,952 KB
testcase_05 AC 1,060 ms
237,624 KB
testcase_06 AC 1,076 ms
237,192 KB
testcase_07 AC 1,088 ms
232,680 KB
testcase_08 AC 652 ms
174,552 KB
testcase_09 AC 676 ms
209,960 KB
testcase_10 AC 1,087 ms
241,184 KB
testcase_11 AC 1,112 ms
249,620 KB
testcase_12 AC 679 ms
194,944 KB
testcase_13 AC 400 ms
105,996 KB
testcase_14 AC 733 ms
256,976 KB
testcase_15 AC 684 ms
209,676 KB
testcase_16 AC 415 ms
110,176 KB
testcase_17 AC 564 ms
161,868 KB
testcase_18 AC 450 ms
153,104 KB
testcase_19 AC 574 ms
130,016 KB
testcase_20 AC 708 ms
250,488 KB
testcase_21 AC 148 ms
78,700 KB
testcase_22 AC 598 ms
174,748 KB
testcase_23 AC 612 ms
194,128 KB
testcase_24 AC 439 ms
160,124 KB
testcase_25 AC 747 ms
258,188 KB
testcase_26 AC 640 ms
191,996 KB
testcase_27 AC 696 ms
221,700 KB
testcase_28 AC 719 ms
212,424 KB
testcase_29 AC 661 ms
223,424 KB
testcase_30 AC 442 ms
128,104 KB
testcase_31 AC 357 ms
93,544 KB
testcase_32 AC 396 ms
129,004 KB
testcase_33 AC 431 ms
128,500 KB
testcase_34 AC 680 ms
194,756 KB
testcase_35 AC 186 ms
79,528 KB
testcase_36 AC 646 ms
202,492 KB
testcase_37 AC 619 ms
180,684 KB
testcase_38 AC 286 ms
97,868 KB
testcase_39 AC 426 ms
130,600 KB
testcase_40 AC 254 ms
81,568 KB
testcase_41 AC 598 ms
144,584 KB
testcase_42 AC 95 ms
71,220 KB
testcase_43 AC 88 ms
71,292 KB
testcase_44 AC 92 ms
71,308 KB
testcase_45 AC 704 ms
230,100 KB
testcase_46 AC 683 ms
229,980 KB
testcase_47 AC 686 ms
230,016 KB
testcase_48 AC 690 ms
230,152 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 UnionFind:
	def __init__(self, n):
		self.par = [i for i in range(n + 1)]
		self.rank = [0] * (n + 1)

	def find(self, x):
		if self.par[x] == x:
			return x
		else:
			self.par[x] = self.find(self.par[x])
			return self.par[x]

	def same_check(self, x, y):
		return self.find(x) == self.find(y)

	def union(self, x, y):
		x = self.find(x); y = self.find(y)
		if self.rank[x] < self.rank[y]:
			self.par[x] = y
		else:
			self.par[y] = x
			if self.rank[x] == self.rank[y]:
				self.rank[x] += 1

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
		self.prev = [None] * N; self.prev[s] = -1
		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.prev[i] = v
					self.Q.append(i)

def topologicalSort(outE, inE, N):

	W = [1] * N
	Q = deque()
	for i in range(N):
		if inE[i] == 0:
			Q.append(i)

	while Q:
		u = Q.popleft()
		for v in outE[u]:
			inE[v] -= 1
			W[v] += W[u]
			if inE[v] == 0:
				Q.append(v)
				

	return W

#処理内容
def main():
	N, M, X = getlist()
	UF = UnionFind(N)
	D = defaultdict(int)
	G = Graph()
	for i in range(M):
		x, y, z = getlist()
		x -= 1; y -= 1
		if not UF.same_check(x, y):
			UF.union(x, y)
			G.add_edge(x, y)
			G.add_edge(y, x)
			D[(10 ** 6) * x + y] = z
			D[(10 ** 6) * y + x] = z

	BF = BFS(G, 0, N)

	outE = defaultdict(list)
	inE = defaultdict(int)
	for i in range(1, N):
		a, b = i, BF.prev[i]
		outE[a].append(b)
		inE[b] += 1

	# print(outE)
	# print(inE)
	W = topologicalSort(outE, inE, N)
	# print(W)
	ans = 0
	for i in range(1, N):
		ans += (N - W[i]) * W[i] * pow(X, D[(10 ** 6) * i + BF.prev[i]], con)
		ans %= con

	print(ans)


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