結果

問題 No.1639 最小通信路
ユーザー sash0sash0
提出日時 2021-08-22 23:26:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,340 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 77,184 KB
最終ジャッジ日時 2024-04-24 14:38:01
合計ジャッジ時間 4,135 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,480 KB
testcase_01 AC 37 ms
52,096 KB
testcase_02 AC 73 ms
72,704 KB
testcase_03 AC 82 ms
76,544 KB
testcase_04 AC 89 ms
77,184 KB
testcase_05 AC 67 ms
68,608 KB
testcase_06 AC 69 ms
69,504 KB
testcase_07 AC 36 ms
52,224 KB
testcase_08 AC 71 ms
71,296 KB
testcase_09 AC 41 ms
53,632 KB
testcase_10 AC 48 ms
55,552 KB
testcase_11 AC 68 ms
69,760 KB
testcase_12 AC 53 ms
61,568 KB
testcase_13 AC 74 ms
71,936 KB
testcase_14 AC 36 ms
52,096 KB
testcase_15 AC 51 ms
55,808 KB
testcase_16 AC 70 ms
69,888 KB
testcase_17 AC 69 ms
71,040 KB
testcase_18 AC 39 ms
53,760 KB
testcase_19 AC 66 ms
68,608 KB
testcase_20 AC 71 ms
71,296 KB
testcase_21 AC 36 ms
52,096 KB
testcase_22 AC 54 ms
62,080 KB
testcase_23 AC 68 ms
69,504 KB
testcase_24 AC 55 ms
61,440 KB
testcase_25 AC 45 ms
54,656 KB
testcase_26 AC 40 ms
53,376 KB
testcase_27 AC 37 ms
52,096 KB
testcase_28 AC 39 ms
52,480 KB
testcase_29 AC 70 ms
70,400 KB
testcase_30 AC 71 ms
72,448 KB
testcase_31 AC 42 ms
54,144 KB
testcase_32 AC 69 ms
70,784 KB
testcase_33 AC 42 ms
53,504 KB
testcase_34 AC 69 ms
70,784 KB
testcase_35 AC 83 ms
76,928 KB
testcase_36 AC 74 ms
70,528 KB
testcase_37 AC 72 ms
71,424 KB
testcase_38 AC 38 ms
52,608 KB
testcase_39 AC 45 ms
55,168 KB
testcase_40 AC 39 ms
52,608 KB
testcase_41 AC 42 ms
53,888 KB
testcase_42 AC 74 ms
72,064 KB
testcase_43 AC 43 ms
53,504 KB
testcase_44 AC 37 ms
52,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self._par = list(i for i in range(n))
        self._rank = list(0 for _ in range(n))
        self._count = list(1 for _ in range(n))

    def parent(self, a):
        if self._par[a] == a:
            return a
        else:
            self._par[a] = self.parent(self._par[a])
            return self._par[a]

    def size(self, a):
        return self._count[self.parent(a)]

    def merge(self, a, b):
        a = self.parent(a)
        b = self.parent(b)

        if a == b:
            return

        if self._rank[a] > self._rank[b]:
            self._par[b] = a
            self._count[a] += self._count[b]
        else:
            self._par[a] = b
            self._count[b] += self._count[a]
            if self._rank[a] == self._rank[b]:
                self._rank[b] += 1

    def same(self, a, b):
        return self.parent(a) == self.parent(b)


if __name__ == '__main__':
    n = int(input().strip())
    li = []
    for i in range(n*(n-1)//2):
        a, b, c = map(int, input().strip().split())
        a -= 1
        b -= 1
        li.append((c, a, b))

    li.sort()

    uf = UnionFind(n)

    ans = 0
    for (c, a, b) in li:
        if uf.size(0) == n:
            break

        if not uf.same(a, b):
            uf.merge(a, b)
            ans = c

    print(ans)
0