結果

問題 No.1207 グラフX
ユーザー 双六双六
提出日時 2020-08-31 23:36:04
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,809 bytes
コンパイル時間 1,198 ms
コンパイル使用メモリ 86,260 KB
実行使用メモリ 371,316 KB
最終ジャッジ日時 2023-08-10 18:07:09
合計ジャッジ時間 40,152 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 730 ms
164,140 KB
testcase_01 AC 666 ms
161,324 KB
testcase_02 AC 660 ms
164,200 KB
testcase_03 AC 677 ms
161,260 KB
testcase_04 AC 684 ms
160,652 KB
testcase_05 TLE -
testcase_06 TLE -
testcase_07 AC 1,949 ms
366,268 KB
testcase_08 AC 682 ms
128,328 KB
testcase_09 AC 733 ms
143,828 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 688 ms
134,096 KB
testcase_13 AC 448 ms
91,180 KB
testcase_14 AC 742 ms
163,548 KB
testcase_15 AC 734 ms
143,656 KB
testcase_16 AC 511 ms
95,148 KB
testcase_17 AC 598 ms
122,044 KB
testcase_18 AC 494 ms
116,828 KB
testcase_19 AC 628 ms
108,292 KB
testcase_20 AC 722 ms
162,504 KB
testcase_21 AC 169 ms
78,792 KB
testcase_22 AC 590 ms
121,700 KB
testcase_23 AC 644 ms
134,844 KB
testcase_24 AC 412 ms
111,828 KB
testcase_25 AC 754 ms
165,080 KB
testcase_26 AC 648 ms
133,044 KB
testcase_27 AC 738 ms
151,264 KB
testcase_28 AC 695 ms
142,972 KB
testcase_29 AC 656 ms
148,392 KB
testcase_30 AC 465 ms
105,044 KB
testcase_31 AC 401 ms
87,324 KB
testcase_32 AC 416 ms
106,116 KB
testcase_33 AC 455 ms
104,640 KB
testcase_34 AC 690 ms
136,748 KB
testcase_35 AC 204 ms
80,728 KB
testcase_36 AC 680 ms
144,136 KB
testcase_37 AC 633 ms
128,372 KB
testcase_38 AC 330 ms
89,080 KB
testcase_39 AC 447 ms
106,924 KB
testcase_40 AC 278 ms
80,540 KB
testcase_41 AC 579 ms
111,084 KB
testcase_42 AC 87 ms
71,208 KB
testcase_43 AC 88 ms
71,216 KB
testcase_44 AC 92 ms
71,320 KB
testcase_45 AC 629 ms
164,328 KB
testcase_46 AC 595 ms
164,368 KB
testcase_47 AC 601 ms
164,296 KB
testcase_48 AC 602 ms
164,360 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)
		self.size = [1] * (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]:
			if self.same_check(x, y) != True:
				self.size[y] += self.size[x]
				self.size[x] = 0
			self.par[x] = y
		else:
			if self.same_check(x, y) != True:
				self.size[x] += self.size[y]
				self.size[y] = 0
			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]
			a, b = list(sorted([node, i]))
			global ans
			ans += (N - W[i]) * W[i] * pow(X, D[(10 ** 6) * a + b], 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

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

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