結果

問題 No.1639 最小通信路
ユーザー first_vilfirst_vil
提出日時 2021-08-06 22:02:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 114 ms / 2,000 ms
コード長 2,148 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 81,716 KB
実行使用メモリ 78,360 KB
最終ジャッジ日時 2023-10-17 03:25:00
合計ジャッジ時間 5,221 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
68,160 KB
testcase_01 AC 61 ms
68,160 KB
testcase_02 AC 103 ms
78,356 KB
testcase_03 AC 110 ms
78,220 KB
testcase_04 AC 114 ms
78,220 KB
testcase_05 AC 97 ms
78,348 KB
testcase_06 AC 98 ms
78,356 KB
testcase_07 AC 62 ms
68,160 KB
testcase_08 AC 102 ms
78,356 KB
testcase_09 AC 64 ms
68,160 KB
testcase_10 AC 72 ms
70,208 KB
testcase_11 AC 99 ms
78,360 KB
testcase_12 AC 76 ms
72,336 KB
testcase_13 AC 99 ms
78,360 KB
testcase_14 AC 62 ms
68,160 KB
testcase_15 AC 72 ms
72,256 KB
testcase_16 AC 98 ms
78,332 KB
testcase_17 AC 100 ms
78,356 KB
testcase_18 AC 65 ms
68,160 KB
testcase_19 AC 98 ms
78,356 KB
testcase_20 AC 99 ms
78,356 KB
testcase_21 AC 60 ms
68,160 KB
testcase_22 AC 77 ms
72,668 KB
testcase_23 AC 96 ms
78,356 KB
testcase_24 AC 75 ms
72,336 KB
testcase_25 AC 68 ms
70,208 KB
testcase_26 AC 63 ms
68,160 KB
testcase_27 AC 61 ms
68,160 KB
testcase_28 AC 60 ms
68,160 KB
testcase_29 AC 98 ms
78,216 KB
testcase_30 AC 101 ms
78,212 KB
testcase_31 AC 68 ms
70,208 KB
testcase_32 AC 99 ms
78,224 KB
testcase_33 AC 63 ms
68,160 KB
testcase_34 AC 102 ms
78,236 KB
testcase_35 AC 104 ms
78,212 KB
testcase_36 AC 101 ms
78,224 KB
testcase_37 AC 101 ms
78,216 KB
testcase_38 AC 61 ms
68,160 KB
testcase_39 AC 69 ms
70,208 KB
testcase_40 AC 62 ms
68,160 KB
testcase_41 AC 65 ms
68,160 KB
testcase_42 AC 102 ms
78,216 KB
testcase_43 AC 66 ms
70,208 KB
testcase_44 AC 61 ms
68,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import typing


class DSU:
    '''
    Implement (union by size) + (path halving)
    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]]
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))
def main():
    n=int(input())
    g=[-1]*n
    edges=[]
    for i in range(n*(n-1)//2):
        a,b,c=map(int,input().split())
        a-=1
        b-=1
        edges.append((c,a,b))
    edges.sort()
    ans=0
    uf=DSU(n)
    for c,a,b in edges:
        if uf.same(a,b):
            continue
        else:
            uf.merge(a,b)
            ans=c
    print(ans)
main()
0