結果

問題 No.1639 最小通信路
ユーザー lloyzlloyz
提出日時 2022-03-24 01:12:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 2,047 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-20 09:17:49
合計ジャッジ時間 3,272 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,880 KB
testcase_01 AC 31 ms
11,008 KB
testcase_02 AC 33 ms
10,880 KB
testcase_03 AC 33 ms
10,880 KB
testcase_04 AC 53 ms
10,880 KB
testcase_05 AC 32 ms
11,008 KB
testcase_06 AC 32 ms
10,880 KB
testcase_07 AC 32 ms
10,880 KB
testcase_08 AC 33 ms
10,880 KB
testcase_09 AC 32 ms
10,880 KB
testcase_10 AC 32 ms
10,880 KB
testcase_11 AC 32 ms
11,008 KB
testcase_12 AC 33 ms
10,880 KB
testcase_13 AC 33 ms
10,880 KB
testcase_14 AC 31 ms
10,880 KB
testcase_15 AC 31 ms
10,880 KB
testcase_16 AC 32 ms
10,880 KB
testcase_17 AC 33 ms
10,880 KB
testcase_18 AC 31 ms
10,880 KB
testcase_19 AC 33 ms
10,880 KB
testcase_20 AC 33 ms
10,880 KB
testcase_21 AC 32 ms
10,880 KB
testcase_22 AC 31 ms
10,880 KB
testcase_23 AC 32 ms
10,880 KB
testcase_24 AC 31 ms
11,008 KB
testcase_25 AC 32 ms
10,880 KB
testcase_26 AC 32 ms
10,880 KB
testcase_27 AC 32 ms
10,880 KB
testcase_28 AC 31 ms
10,880 KB
testcase_29 AC 32 ms
10,880 KB
testcase_30 AC 32 ms
10,880 KB
testcase_31 AC 33 ms
11,008 KB
testcase_32 AC 33 ms
10,880 KB
testcase_33 AC 32 ms
10,880 KB
testcase_34 AC 32 ms
10,880 KB
testcase_35 AC 34 ms
10,880 KB
testcase_36 AC 33 ms
10,880 KB
testcase_37 AC 32 ms
10,880 KB
testcase_38 AC 32 ms
10,880 KB
testcase_39 AC 32 ms
10,880 KB
testcase_40 AC 30 ms
10,880 KB
testcase_41 AC 31 ms
11,008 KB
testcase_42 AC 31 ms
10,880 KB
testcase_43 AC 31 ms
10,880 KB
testcase_44 AC 31 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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())

n = int(input())
UF = UnionFind(n)
for _ in range(n * (n - 1) // 2):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    if UF.same(a, b):
        continue
    UF.union(a, b)
    if UF.group_count() == 1:
        print(c)
        break
0