結果

問題 No.1639 最小通信路
ユーザー takeya_okinotakeya_okino
提出日時 2021-09-25 14:36:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 1,558 bytes
コンパイル時間 602 ms
コンパイル使用メモリ 87,068 KB
実行使用メモリ 79,952 KB
最終ジャッジ日時 2023-09-18 23:02:05
合計ジャッジ時間 8,194 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,632 KB
testcase_01 AC 91 ms
71,700 KB
testcase_02 AC 127 ms
78,772 KB
testcase_03 AC 131 ms
79,656 KB
testcase_04 AC 133 ms
79,292 KB
testcase_05 AC 121 ms
79,952 KB
testcase_06 AC 121 ms
79,532 KB
testcase_07 AC 88 ms
71,876 KB
testcase_08 AC 122 ms
79,184 KB
testcase_09 AC 94 ms
72,448 KB
testcase_10 AC 103 ms
77,224 KB
testcase_11 AC 122 ms
79,188 KB
testcase_12 AC 104 ms
77,024 KB
testcase_13 AC 123 ms
78,900 KB
testcase_14 AC 89 ms
71,824 KB
testcase_15 AC 107 ms
77,016 KB
testcase_16 AC 124 ms
79,376 KB
testcase_17 AC 124 ms
79,216 KB
testcase_18 AC 96 ms
72,244 KB
testcase_19 AC 122 ms
79,528 KB
testcase_20 AC 125 ms
79,192 KB
testcase_21 AC 89 ms
71,768 KB
testcase_22 AC 109 ms
77,208 KB
testcase_23 AC 124 ms
79,572 KB
testcase_24 AC 109 ms
77,072 KB
testcase_25 AC 101 ms
76,352 KB
testcase_26 AC 95 ms
72,236 KB
testcase_27 AC 91 ms
71,544 KB
testcase_28 AC 89 ms
71,540 KB
testcase_29 AC 127 ms
79,232 KB
testcase_30 AC 133 ms
78,828 KB
testcase_31 AC 98 ms
72,276 KB
testcase_32 AC 129 ms
78,976 KB
testcase_33 AC 98 ms
72,416 KB
testcase_34 AC 127 ms
79,320 KB
testcase_35 AC 137 ms
78,404 KB
testcase_36 AC 127 ms
79,392 KB
testcase_37 AC 128 ms
79,256 KB
testcase_38 AC 91 ms
71,788 KB
testcase_39 AC 103 ms
76,888 KB
testcase_40 AC 93 ms
71,600 KB
testcase_41 AC 98 ms
72,544 KB
testcase_42 AC 127 ms
78,672 KB
testcase_43 AC 99 ms
72,632 KB
testcase_44 AC 93 ms
71,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
A=[]

for i in range((n * (n - 1)) // 2):
  a, b, c = map(int, input().split())
  A.append([c, a - 1, b - 1])

A.sort()
 
from collections import defaultdict
 
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
 
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
 
    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
 
        self.parents[x] += self.parents[y]
        self.parents[y] = x
 
    def size(self, x):
        return -self.parents[self.find(x)]
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
 
    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]
 
    def group_count(self):
        return len(self.roots())
 
    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members
 
    def __str__(self):
   	     return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

ans = 0

uf = UnionFind(n)

for [c, a, b] in A:
  if not uf.same(a,b):
    ans = c
    uf.union(a,b)

print(ans)
0