結果

問題 No.1639 最小通信路
ユーザー 河野竜也河野竜也
提出日時 2021-09-12 18:26:05
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,452 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 10,964 KB
実行使用メモリ 8,728 KB
最終ジャッジ日時 2023-09-06 15:12:44
合計ジャッジ時間 3,890 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,664 KB
testcase_01 AC 17 ms
8,668 KB
testcase_02 AC 20 ms
8,556 KB
testcase_03 AC 19 ms
8,656 KB
testcase_04 AC 53 ms
8,564 KB
testcase_05 AC 18 ms
8,600 KB
testcase_06 AC 19 ms
8,724 KB
testcase_07 AC 18 ms
8,676 KB
testcase_08 AC 19 ms
8,544 KB
testcase_09 AC 18 ms
8,672 KB
testcase_10 AC 18 ms
8,668 KB
testcase_11 AC 19 ms
8,536 KB
testcase_12 AC 18 ms
8,532 KB
testcase_13 AC 19 ms
8,544 KB
testcase_14 AC 17 ms
8,708 KB
testcase_15 AC 18 ms
8,532 KB
testcase_16 AC 19 ms
8,672 KB
testcase_17 AC 19 ms
8,564 KB
testcase_18 AC 18 ms
8,720 KB
testcase_19 AC 18 ms
8,712 KB
testcase_20 AC 20 ms
8,704 KB
testcase_21 AC 18 ms
8,556 KB
testcase_22 AC 18 ms
8,532 KB
testcase_23 AC 18 ms
8,620 KB
testcase_24 AC 18 ms
8,568 KB
testcase_25 AC 18 ms
8,552 KB
testcase_26 AC 17 ms
8,568 KB
testcase_27 AC 17 ms
8,704 KB
testcase_28 AC 17 ms
8,680 KB
testcase_29 AC 17 ms
8,708 KB
testcase_30 AC 18 ms
8,564 KB
testcase_31 AC 17 ms
8,704 KB
testcase_32 AC 18 ms
8,536 KB
testcase_33 AC 18 ms
8,532 KB
testcase_34 AC 18 ms
8,596 KB
testcase_35 AC 20 ms
8,600 KB
testcase_36 AC 19 ms
8,556 KB
testcase_37 AC 18 ms
8,532 KB
testcase_38 AC 18 ms
8,544 KB
testcase_39 AC 18 ms
8,564 KB
testcase_40 AC 17 ms
8,540 KB
testcase_41 AC 17 ms
8,672 KB
testcase_42 AC 18 ms
8,620 KB
testcase_43 AC 18 ms
8,536 KB
testcase_44 AC 19 ms
8,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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())
n=int(input())
uf=UnionFind(n)
for i in range(n*(n-1)//2):
  a,b,c=map(int,input().split())
  uf.union(a-1,b-1)
  if uf.group_count()==1:
    print(c)
    break
0