結果

問題 No.1639 最小通信路
ユーザー 👑 rin204rin204
提出日時 2021-09-03 18:52:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,519 bytes
コンパイル時間 1,757 ms
コンパイル使用メモリ 86,480 KB
実行使用メモリ 78,080 KB
最終ジャッジ日時 2023-08-21 16:47:54
合計ジャッジ時間 8,998 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
71,272 KB
testcase_01 AC 84 ms
70,968 KB
testcase_02 AC 144 ms
77,788 KB
testcase_03 AC 128 ms
77,768 KB
testcase_04 AC 129 ms
78,000 KB
testcase_05 AC 115 ms
77,860 KB
testcase_06 AC 120 ms
77,668 KB
testcase_07 AC 82 ms
71,300 KB
testcase_08 AC 124 ms
77,524 KB
testcase_09 AC 86 ms
71,336 KB
testcase_10 AC 94 ms
75,728 KB
testcase_11 AC 119 ms
77,416 KB
testcase_12 AC 98 ms
76,164 KB
testcase_13 AC 120 ms
77,796 KB
testcase_14 AC 78 ms
71,292 KB
testcase_15 AC 94 ms
75,740 KB
testcase_16 AC 118 ms
77,860 KB
testcase_17 AC 118 ms
77,776 KB
testcase_18 AC 82 ms
71,060 KB
testcase_19 AC 116 ms
77,780 KB
testcase_20 AC 117 ms
77,724 KB
testcase_21 AC 80 ms
71,300 KB
testcase_22 AC 102 ms
76,180 KB
testcase_23 AC 119 ms
77,624 KB
testcase_24 AC 99 ms
75,964 KB
testcase_25 AC 88 ms
75,024 KB
testcase_26 AC 82 ms
71,340 KB
testcase_27 AC 81 ms
71,048 KB
testcase_28 AC 79 ms
71,304 KB
testcase_29 AC 120 ms
77,820 KB
testcase_30 AC 124 ms
77,752 KB
testcase_31 AC 83 ms
71,228 KB
testcase_32 AC 120 ms
77,892 KB
testcase_33 AC 82 ms
71,276 KB
testcase_34 AC 120 ms
78,080 KB
testcase_35 AC 126 ms
77,960 KB
testcase_36 AC 120 ms
77,812 KB
testcase_37 AC 127 ms
77,628 KB
testcase_38 AC 82 ms
71,312 KB
testcase_39 AC 92 ms
75,320 KB
testcase_40 AC 82 ms
71,348 KB
testcase_41 AC 84 ms
71,060 KB
testcase_42 AC 122 ms
77,976 KB
testcase_43 AC 85 ms
70,944 KB
testcase_44 AC 79 ms
71,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = 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
        self.group -= 1
        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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n = int(input())
abc = [list(map(int, input().split())) for _ in range(n * (n - 1) // 2)]
abc.sort(key = lambda x:x[2])
UF = UnionFind(n)
for a, b, c in abc:
    a -= 1
    b -= 1
    if not UF.same(a, b):
        UF.union(a, b)
        ans = c
print(ans)

0