結果

問題 No.1639 最小通信路
コンテスト
ユーザー ryusuke
提出日時 2022-01-26 17:56:40
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 803 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 482 ms
コンパイル使用メモリ 85,376 KB
実行使用メモリ 82,944 KB
最終ジャッジ日時 2026-05-29 11:07:51
合計ジャッジ時間 4,500 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# クラスカル法

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