結果

問題 No.1207 グラフX
ユーザー 双六双六
提出日時 2020-08-31 23:40:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,983 ms / 2,000 ms
コード長 1,598 bytes
コンパイル時間 248 ms
コンパイル使用メモリ 87,196 KB
実行使用メモリ 375,148 KB
最終ジャッジ日時 2023-08-10 18:10:32
合計ジャッジ時間 35,509 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 608 ms
172,332 KB
testcase_01 AC 611 ms
172,344 KB
testcase_02 AC 620 ms
172,848 KB
testcase_03 AC 604 ms
172,360 KB
testcase_04 AC 616 ms
172,236 KB
testcase_05 AC 1,868 ms
368,792 KB
testcase_06 AC 1,983 ms
370,996 KB
testcase_07 AC 1,679 ms
375,148 KB
testcase_08 AC 643 ms
137,096 KB
testcase_09 AC 636 ms
153,612 KB
testcase_10 AC 1,756 ms
347,316 KB
testcase_11 AC 1,689 ms
369,012 KB
testcase_12 AC 613 ms
139,300 KB
testcase_13 AC 395 ms
93,628 KB
testcase_14 AC 660 ms
176,796 KB
testcase_15 AC 645 ms
145,676 KB
testcase_16 AC 422 ms
97,472 KB
testcase_17 AC 547 ms
129,224 KB
testcase_18 AC 432 ms
123,680 KB
testcase_19 AC 546 ms
111,432 KB
testcase_20 AC 647 ms
178,772 KB
testcase_21 AC 145 ms
78,840 KB
testcase_22 AC 548 ms
128,328 KB
testcase_23 AC 558 ms
136,460 KB
testcase_24 AC 381 ms
117,620 KB
testcase_25 AC 650 ms
177,316 KB
testcase_26 AC 563 ms
143,168 KB
testcase_27 AC 635 ms
159,520 KB
testcase_28 AC 637 ms
153,188 KB
testcase_29 AC 598 ms
158,072 KB
testcase_30 AC 418 ms
106,304 KB
testcase_31 AC 369 ms
88,872 KB
testcase_32 AC 378 ms
107,356 KB
testcase_33 AC 420 ms
108,388 KB
testcase_34 AC 653 ms
143,620 KB
testcase_35 AC 186 ms
79,552 KB
testcase_36 AC 598 ms
150,724 KB
testcase_37 AC 565 ms
134,276 KB
testcase_38 AC 292 ms
90,972 KB
testcase_39 AC 393 ms
109,744 KB
testcase_40 AC 235 ms
81,836 KB
testcase_41 AC 518 ms
113,684 KB
testcase_42 AC 89 ms
71,660 KB
testcase_43 AC 87 ms
71,616 KB
testcase_44 AC 88 ms
71,524 KB
testcase_45 AC 576 ms
171,928 KB
testcase_46 AC 578 ms
171,808 KB
testcase_47 AC 587 ms
172,280 KB
testcase_48 AC 587 ms
172,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
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)


ans = 0
def DFS(N, G, W, visit, node, D, X):
	for i in G.graph[node]:
		if visit[i] != 1:
			visit[i] = 1
			DFS(N, G, W, visit, i, D, X)
			W[node] += W[i]
			global ans
			ans += (N - W[i]) * W[i] * pow(X, D[(10 ** 6) * node + i], con)
			ans %= con

#処理内容
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
		# x, y = list(sorted([x, y]))
		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

	W = [1] * N
	visit = [0] * N
	visit[0] = 1
	DFS(N, G, W, visit, 0, D, X)
	print(ans)

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