結果

問題 No.1207 グラフX
ユーザー 双六双六
提出日時 2020-08-31 23:36:46
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,809 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 141,316 KB
最終ジャッジ日時 2024-04-28 11:34:48
合計ジャッジ時間 72,621 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 AC 1,438 ms
55,012 KB
testcase_09 AC 1,603 ms
64,104 KB
testcase_10 TLE -
testcase_11 TLE -
testcase_12 AC 1,456 ms
58,176 KB
testcase_13 AC 629 ms
22,280 KB
testcase_14 TLE -
testcase_15 AC 1,609 ms
62,780 KB
testcase_16 AC 633 ms
25,348 KB
testcase_17 AC 1,237 ms
52,168 KB
testcase_18 AC 1,152 ms
52,972 KB
testcase_19 AC 983 ms
35,824 KB
testcase_20 TLE -
testcase_21 AC 109 ms
11,392 KB
testcase_22 AC 1,246 ms
52,744 KB
testcase_23 AC 1,368 ms
58,228 KB
testcase_24 AC 999 ms
49,540 KB
testcase_25 TLE -
testcase_26 AC 1,505 ms
61,272 KB
testcase_27 AC 1,779 ms
70,676 KB
testcase_28 AC 1,676 ms
64,568 KB
testcase_29 AC 1,724 ms
69,140 KB
testcase_30 AC 773 ms
34,852 KB
testcase_31 AC 484 ms
17,788 KB
testcase_32 AC 724 ms
36,728 KB
testcase_33 AC 753 ms
35,848 KB
testcase_34 AC 1,552 ms
62,248 KB
testcase_35 AC 116 ms
12,032 KB
testcase_36 AC 1,567 ms
63,948 KB
testcase_37 AC 1,313 ms
54,804 KB
testcase_38 AC 338 ms
21,592 KB
testcase_39 AC 800 ms
37,944 KB
testcase_40 AC 341 ms
12,544 KB
testcase_41 AC 997 ms
36,364 KB
testcase_42 AC 27 ms
10,752 KB
testcase_43 AC 27 ms
11,008 KB
testcase_44 AC 27 ms
11,008 KB
testcase_45 AC 1,996 ms
78,256 KB
testcase_46 AC 1,901 ms
78,512 KB
testcase_47 AC 1,886 ms
78,512 KB
testcase_48 AC 1,893 ms
78,260 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