結果

問題 No.1639 最小通信路
ユーザー MasKoaTS
提出日時 2021-10-03 18:58:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 2,765 bytes
コンパイル時間 297 ms
コンパイル使用メモリ 82,544 KB
実行使用メモリ 89,976 KB
最終ジャッジ日時 2024-07-21 13:56:46
合計ジャッジ時間 8,254 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools as iter
import collections as coll
import heapq as hq
import bisect as bis
from decimal import Decimal as dec
from copy import deepcopy as dcopy
import math
import sys
sys.setrecursionlimit(10**6)
def input():
    return sys.stdin.readline().rstrip()
def getN():
	return int(sys.stdin.readline().rstrip())
def getNs():
	return map(int,sys.stdin.readline().rstrip().split())
def getList():
	return list(map(int,sys.stdin.readline().rstrip().split()))
def strinps(n):
	return [sys.stdin.readline().rstrip() for _ in range(n)]
pi = 3.141592653589793
mod = 10**9+7
MOD = 998244353
INF = math.inf
dx = [1,0,-1,0];	dy = [0,1,0,-1]

"""
Union-Find
from : https://github.com/customaddone/beginPython/blob/master/cgi-bin/library/unionfind/unionfind.py
ref : https://algo-logic.info/union-find-tree/
"""

class UnionFind():
	#Uni = UnionFind(n) のようにする
	def __init__(self, n):
		self.n = n
		self.parents = [-1] * n

	#xの親(親がいないときは自身の番号を返す)
	def find(self, x):
		if self.parents[x] < 0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	#xとyを関係付ける
	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)

		if x == y:
			return

		if self.parents[x] > self.parents[y]:
			x, y = y, x
		# if x > y: # よりrootのインデックスが小さい方が親
			# x, y = y, x

		self.parents[x] += self.parents[y]
		self.parents[y] = x

	#xとyが同じ組に属するかどうか
	def same(self, x, y):
		return self.find(x) == self.find(y)

	#xが属する組の大きさ
	def size(self, x):
		return -self.parents[self.find(x)]

	#xが属する組の全要素をリストとして取得
	def members(self, x):
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i) == root]

	#Union-Find木の根に当たる要素全てをリストとして取得
	def roots(self):
		return [i for i, x in enumerate(self.parents) if x < 0]

	#Union-Find木の根とそれに属する組の全要素
	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()}


"""
クラスカル法によって最小全域木を求める
ref : https://algo-logic.info/kruskal-mst/
"""

"""
(入力例)
V, E = map(int,sys.stdin.readline().rstrip().split())
edges = []
for i in range(E):
	s, t, w = map(int,sys.stdin.readline().rstrip().split())
	edges.append((w, s, t))
edges.sort()
"""

def kruskal(n, edges):
	U = UnionFind(n)
	res = 0
	for e in edges:
		w, s, t = e
		if not U.same(s, t):
			res = max(res,w)
			U.union(s, t)
	return res


"""
Main Code
"""

n = getN()
e = n*(n-1)//2
edges = []
for i in range(e):
	s, t, w = map(int,sys.stdin.readline().split())
	edges.append((w, s-1, t-1))
edges.sort()
print(kruskal(n,edges))
0