結果

問題 No.1207 グラフX
ユーザー 双六双六
提出日時 2020-09-01 00:07:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,318 ms / 2,000 ms
コード長 2,232 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 265,068 KB
最終ジャッジ日時 2024-04-28 11:41:42
合計ジャッジ時間 37,084 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 822 ms
235,076 KB
testcase_01 AC 835 ms
238,268 KB
testcase_02 AC 827 ms
236,512 KB
testcase_03 AC 839 ms
237,636 KB
testcase_04 AC 849 ms
237,868 KB
testcase_05 AC 1,318 ms
263,204 KB
testcase_06 AC 1,313 ms
264,304 KB
testcase_07 AC 1,271 ms
260,012 KB
testcase_08 AC 780 ms
171,640 KB
testcase_09 AC 805 ms
207,964 KB
testcase_10 AC 1,299 ms
260,752 KB
testcase_11 AC 1,309 ms
265,068 KB
testcase_12 AC 765 ms
193,036 KB
testcase_13 AC 405 ms
99,712 KB
testcase_14 AC 828 ms
242,772 KB
testcase_15 AC 770 ms
210,908 KB
testcase_16 AC 443 ms
112,000 KB
testcase_17 AC 611 ms
160,180 KB
testcase_18 AC 472 ms
151,248 KB
testcase_19 AC 616 ms
139,700 KB
testcase_20 AC 799 ms
257,744 KB
testcase_21 AC 127 ms
77,324 KB
testcase_22 AC 678 ms
172,496 KB
testcase_23 AC 674 ms
187,024 KB
testcase_24 AC 444 ms
145,152 KB
testcase_25 AC 847 ms
245,044 KB
testcase_26 AC 661 ms
189,980 KB
testcase_27 AC 770 ms
225,688 KB
testcase_28 AC 758 ms
209,596 KB
testcase_29 AC 698 ms
215,576 KB
testcase_30 AC 451 ms
125,944 KB
testcase_31 AC 338 ms
91,996 KB
testcase_32 AC 388 ms
127,268 KB
testcase_33 AC 423 ms
126,616 KB
testcase_34 AC 735 ms
195,428 KB
testcase_35 AC 159 ms
78,080 KB
testcase_36 AC 696 ms
194,160 KB
testcase_37 AC 650 ms
179,920 KB
testcase_38 AC 282 ms
96,128 KB
testcase_39 AC 419 ms
127,824 KB
testcase_40 AC 235 ms
79,232 KB
testcase_41 AC 586 ms
144,000 KB
testcase_42 AC 45 ms
54,400 KB
testcase_43 AC 45 ms
54,528 KB
testcase_44 AC 45 ms
54,272 KB
testcase_45 AC 725 ms
236,284 KB
testcase_46 AC 755 ms
236,844 KB
testcase_47 AC 726 ms
236,464 KB
testcase_48 AC 720 ms
236,852 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