結果

問題 No.1639 最小通信路
ユーザー ryusuke
提出日時 2022-01-26 17:56:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 803 bytes
コンパイル時間 676 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 77,440 KB
最終ジャッジ日時 2024-12-23 10:12:33
合計ジャッジ時間 5,590 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

# クラスカル法

class UnionFind:
  def __init__(self, n):
    self.n = n
    self.p = [-1] * n

  def leader(self, a):
    while self.p[a] >= 0:
      a = self.p[a]
    return a

  def merge(self, a, b):
    x = self.leader(a)
    y = self.leader(b)
    if x == y: return x
    if self.p[x] > self.p[y]:
      x, y = y, x
    self.p[x] += self.p[y]
    self.p[y] = x
    return x

  def same(self, a, b): return self.leader(a) == self.leader(b)

  def size(self, a): return -self.p[self.leader(a)]

n = int(input())
uf = UnionFind(n)
# (a, b, cost)の無向グラフ
g = [list(map(int, input().split())) for _ in range(n * (n - 1) // 2)]
g.sort(key=lambda x: x[2])

ans = 0
for i, j, cost in g:
    if uf.same(i - 1, j - 1): continue
    uf.merge(i - 1, j - 1)
    ans = max(ans, cost)

print(ans)
0