結果

問題 No.1639 最小通信路
ユーザー kohei2019kohei2019
提出日時 2022-06-27 22:58:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 128 ms / 2,000 ms
コード長 1,795 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 79,100 KB
最終ジャッジ日時 2024-04-30 11:46:39
合計ジャッジ時間 5,039 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,140 KB
testcase_01 AC 39 ms
55,588 KB
testcase_02 AC 109 ms
78,444 KB
testcase_03 AC 128 ms
79,100 KB
testcase_04 AC 119 ms
78,636 KB
testcase_05 AC 87 ms
77,460 KB
testcase_06 AC 91 ms
77,280 KB
testcase_07 AC 40 ms
55,144 KB
testcase_08 AC 109 ms
78,256 KB
testcase_09 AC 47 ms
63,468 KB
testcase_10 AC 65 ms
68,924 KB
testcase_11 AC 106 ms
77,804 KB
testcase_12 AC 66 ms
72,628 KB
testcase_13 AC 113 ms
78,416 KB
testcase_14 AC 41 ms
56,172 KB
testcase_15 AC 62 ms
69,032 KB
testcase_16 AC 105 ms
77,688 KB
testcase_17 AC 111 ms
78,292 KB
testcase_18 AC 49 ms
62,740 KB
testcase_19 AC 91 ms
76,880 KB
testcase_20 AC 112 ms
78,084 KB
testcase_21 AC 42 ms
55,196 KB
testcase_22 AC 72 ms
73,924 KB
testcase_23 AC 93 ms
77,232 KB
testcase_24 AC 70 ms
72,576 KB
testcase_25 AC 57 ms
65,648 KB
testcase_26 AC 46 ms
56,564 KB
testcase_27 AC 41 ms
55,060 KB
testcase_28 AC 43 ms
55,256 KB
testcase_29 AC 94 ms
77,148 KB
testcase_30 AC 104 ms
78,168 KB
testcase_31 AC 52 ms
64,216 KB
testcase_32 AC 93 ms
77,128 KB
testcase_33 AC 48 ms
63,920 KB
testcase_34 AC 93 ms
77,632 KB
testcase_35 AC 116 ms
78,464 KB
testcase_36 AC 91 ms
77,116 KB
testcase_37 AC 93 ms
77,468 KB
testcase_38 AC 39 ms
54,928 KB
testcase_39 AC 55 ms
66,224 KB
testcase_40 AC 41 ms
57,000 KB
testcase_41 AC 47 ms
63,736 KB
testcase_42 AC 99 ms
77,388 KB
testcase_43 AC 50 ms
63,584 KB
testcase_44 AC 41 ms
55,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
#unionfind経路圧縮あり
import collections
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = list(range(n))
        self.size0 = [1]*(n)
        self.roots = n

    def find(self, x):
        if self.parents[x] == x:
            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 self.parents[x] > self.parents[y]:
            x, y = y, x
        if x == y:
            return
        self.size0[x] += self.size0[y]
        self.roots -= 1
        self.parents[y] = x

    def size(self, x):#O(1)xが含まれる集合の要素数
        return self.size0[self.find(x)]

    def same(self, x, y):#O(1)
        return self.find(x) == self.find(y)

    def membersf(self, x):#取り出し部分はO(N)
        p = self.find(x)
        ret = []
        for i in range(self.n):
            if self.find(i) == p:
                ret.append(i)
        return ret

    def rootsf(self):#根の要素O(N)
        ret = []
        for i in range(self.n):
            if self.find(i) == i:
                ret.append(i)
        return ret

    def group_count(self):#根の数O(1)
        return self.roots

    def all_group_members(self):#O(N)
        ret = collections.defaultdict(lambda:[])
        for i in range(self.n):
            ret[self.find(i)].append(i)
        return ret

N = int(input())
h = []
for i in range(N*(N-1)//2):
    a,b,C = map(int,input().split())
    a -= 1
    b -= 1
    h.append((C,a,b)) 

UF = UnionFind(N)
heapq.heapify(h)
rmax = 0
while h:
    C,a,b = heapq.heappop(h)
    if UF.same(a,b):
        continue
    rmax = max(rmax,C)
    UF.union(a,b)
print(rmax)
0