結果

問題 No.1639 最小通信路
ユーザー ntudantuda
提出日時 2021-08-18 22:19:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 1,470 bytes
コンパイル時間 530 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 77,568 KB
最終ジャッジ日時 2024-04-20 03:15:43
合計ジャッジ時間 4,941 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,480 KB
testcase_01 AC 44 ms
52,352 KB
testcase_02 AC 75 ms
77,096 KB
testcase_03 AC 109 ms
77,184 KB
testcase_04 AC 83 ms
77,244 KB
testcase_05 AC 71 ms
72,064 KB
testcase_06 AC 70 ms
73,216 KB
testcase_07 AC 40 ms
52,736 KB
testcase_08 AC 72 ms
76,928 KB
testcase_09 AC 41 ms
53,632 KB
testcase_10 AC 51 ms
62,976 KB
testcase_11 AC 80 ms
74,076 KB
testcase_12 AC 62 ms
65,280 KB
testcase_13 AC 72 ms
77,056 KB
testcase_14 AC 43 ms
52,352 KB
testcase_15 AC 52 ms
63,616 KB
testcase_16 AC 72 ms
74,624 KB
testcase_17 AC 76 ms
77,184 KB
testcase_18 AC 40 ms
53,504 KB
testcase_19 AC 74 ms
72,064 KB
testcase_20 AC 76 ms
76,888 KB
testcase_21 AC 37 ms
52,480 KB
testcase_22 AC 55 ms
65,664 KB
testcase_23 AC 86 ms
73,600 KB
testcase_24 AC 57 ms
65,024 KB
testcase_25 AC 57 ms
60,544 KB
testcase_26 AC 47 ms
53,632 KB
testcase_27 AC 34 ms
52,736 KB
testcase_28 AC 37 ms
52,736 KB
testcase_29 AC 77 ms
74,924 KB
testcase_30 AC 82 ms
77,056 KB
testcase_31 AC 52 ms
55,168 KB
testcase_32 AC 78 ms
76,928 KB
testcase_33 AC 41 ms
53,888 KB
testcase_34 AC 81 ms
76,928 KB
testcase_35 AC 102 ms
77,568 KB
testcase_36 AC 76 ms
75,008 KB
testcase_37 AC 94 ms
76,980 KB
testcase_38 AC 41 ms
52,992 KB
testcase_39 AC 55 ms
61,312 KB
testcase_40 AC 43 ms
52,992 KB
testcase_41 AC 43 ms
53,888 KB
testcase_42 AC 86 ms
77,184 KB
testcase_43 AC 41 ms
54,400 KB
testcase_44 AC 37 ms
52,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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):
        return {r: self.members(r) for r in self.roots()}
 
    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)

ctd = set()
ans = 0
path = []
for abc in ABC:
    a,b,c = abc
    a -= 1
    b -= 1
    if uf.find(a) != uf.find(b):
        ans = max(ans,c)
        uf.union(a,b)
print(ans)
0