結果

問題 No.1639 最小通信路
ユーザー lam6er
提出日時 2025-03-20 18:42:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 55 ms / 2,000 ms
コード長 1,312 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 82,104 KB
実行使用メモリ 70,896 KB
最終ジャッジ日時 2025-03-20 18:43:02
合計ジャッジ時間 3,833 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size + 1))  # Nodes are 1-based
        self.rank = [0] * (size + 1)
    
    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]
    
    def union(self, x, y):
        x_root = self.find(x)
        y_root = self.find(y)
        if x_root == y_root:
            return False  # Already connected
        if self.rank[x_root] < self.rank[y_root]:
            self.parent[x_root] = y_root
        else:
            self.parent[y_root] = x_root
            if self.rank[x_root] == self.rank[y_root]:
                self.rank[x_root] += 1
        return True

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    edges = []
    for _ in range(N * (N - 1) // 2):
        a = int(input[idx])
        b = int(input[idx + 1])
        c = int(input[idx + 2])
        edges.append((c, a, b))
        idx += 3
    
    edges.sort()
    uf = UnionFind(N)
    max_r = 0
    count = 0
    for c, a, b in edges:
        if uf.union(a, b):
            max_r = c
            count += 1
            if count == N - 1:
                break
    
    print(max_r)

if __name__ == "__main__":
    main()
0