結果

問題 No.1207 グラフX
ユーザー 双六双六
提出日時 2020-08-31 23:40:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,641 ms / 2,000 ms
コード長 1,598 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 368,812 KB
最終ジャッジ日時 2024-04-28 11:36:32
合計ジャッジ時間 30,727 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 584 ms
172,124 KB
testcase_01 AC 589 ms
171,472 KB
testcase_02 AC 585 ms
171,904 KB
testcase_03 AC 587 ms
172,140 KB
testcase_04 AC 595 ms
171,596 KB
testcase_05 AC 1,641 ms
367,480 KB
testcase_06 AC 1,569 ms
366,432 KB
testcase_07 AC 1,519 ms
363,552 KB
testcase_08 AC 578 ms
136,388 KB
testcase_09 AC 577 ms
150,076 KB
testcase_10 AC 1,595 ms
325,136 KB
testcase_11 AC 1,562 ms
368,812 KB
testcase_12 AC 558 ms
136,704 KB
testcase_13 AC 347 ms
91,904 KB
testcase_14 AC 619 ms
172,440 KB
testcase_15 AC 605 ms
147,212 KB
testcase_16 AC 355 ms
94,720 KB
testcase_17 AC 501 ms
127,316 KB
testcase_18 AC 378 ms
119,400 KB
testcase_19 AC 477 ms
109,316 KB
testcase_20 AC 609 ms
175,272 KB
testcase_21 AC 109 ms
77,312 KB
testcase_22 AC 481 ms
125,800 KB
testcase_23 AC 478 ms
133,592 KB
testcase_24 AC 341 ms
117,120 KB
testcase_25 AC 630 ms
174,056 KB
testcase_26 AC 528 ms
140,968 KB
testcase_27 AC 594 ms
158,532 KB
testcase_28 AC 618 ms
150,476 KB
testcase_29 AC 542 ms
157,068 KB
testcase_30 AC 375 ms
104,876 KB
testcase_31 AC 316 ms
86,528 KB
testcase_32 AC 316 ms
105,360 KB
testcase_33 AC 352 ms
105,416 KB
testcase_34 AC 590 ms
140,920 KB
testcase_35 AC 145 ms
78,080 KB
testcase_36 AC 556 ms
149,688 KB
testcase_37 AC 504 ms
132,444 KB
testcase_38 AC 239 ms
88,448 KB
testcase_39 AC 339 ms
107,576 KB
testcase_40 AC 196 ms
78,976 KB
testcase_41 AC 458 ms
111,992 KB
testcase_42 AC 40 ms
54,016 KB
testcase_43 AC 40 ms
54,016 KB
testcase_44 AC 40 ms
53,760 KB
testcase_45 AC 536 ms
172,612 KB
testcase_46 AC 534 ms
172,624 KB
testcase_47 AC 546 ms
172,500 KB
testcase_48 AC 538 ms
172,372 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