結果

問題 No.1207 グラフX
ユーザー 双六双六
提出日時 2020-08-31 23:38:23
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,599 bytes
コンパイル時間 1,522 ms
コンパイル使用メモリ 86,752 KB
実行使用メモリ 364,140 KB
最終ジャッジ日時 2023-08-10 18:09:29
合計ジャッジ時間 38,682 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 694 ms
162,856 KB
testcase_01 AC 722 ms
161,728 KB
testcase_02 AC 707 ms
162,928 KB
testcase_03 AC 708 ms
159,864 KB
testcase_04 AC 707 ms
161,728 KB
testcase_05 TLE -
testcase_06 AC 1,895 ms
362,688 KB
testcase_07 AC 1,980 ms
362,924 KB
testcase_08 AC 690 ms
126,628 KB
testcase_09 AC 746 ms
140,556 KB
testcase_10 TLE -
testcase_11 AC 1,951 ms
364,140 KB
testcase_12 AC 665 ms
133,348 KB
testcase_13 AC 452 ms
91,236 KB
testcase_14 AC 769 ms
163,716 KB
testcase_15 AC 754 ms
142,080 KB
testcase_16 AC 487 ms
95,896 KB
testcase_17 AC 578 ms
120,352 KB
testcase_18 AC 472 ms
116,564 KB
testcase_19 AC 629 ms
105,308 KB
testcase_20 AC 734 ms
162,728 KB
testcase_21 AC 166 ms
79,476 KB
testcase_22 AC 575 ms
121,080 KB
testcase_23 AC 629 ms
134,096 KB
testcase_24 AC 424 ms
111,328 KB
testcase_25 AC 742 ms
163,884 KB
testcase_26 AC 648 ms
134,948 KB
testcase_27 AC 743 ms
150,408 KB
testcase_28 AC 711 ms
141,732 KB
testcase_29 AC 669 ms
147,888 KB
testcase_30 AC 464 ms
103,152 KB
testcase_31 AC 406 ms
87,764 KB
testcase_32 AC 437 ms
105,060 KB
testcase_33 AC 477 ms
103,592 KB
testcase_34 AC 712 ms
137,000 KB
testcase_35 AC 199 ms
79,984 KB
testcase_36 AC 701 ms
139,168 KB
testcase_37 AC 648 ms
126,088 KB
testcase_38 AC 332 ms
88,544 KB
testcase_39 AC 434 ms
106,296 KB
testcase_40 AC 264 ms
81,668 KB
testcase_41 AC 581 ms
110,608 KB
testcase_42 AC 89 ms
71,000 KB
testcase_43 AC 87 ms
71,284 KB
testcase_44 AC 88 ms
71,112 KB
testcase_45 AC 633 ms
163,164 KB
testcase_46 AC 626 ms
163,460 KB
testcase_47 AC 619 ms
163,156 KB
testcase_48 AC 618 ms
163,408 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]
			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