結果

問題 No.1639 最小通信路
ユーザー ygd.ygd.
提出日時 2021-08-06 21:54:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,681 bytes
コンパイル時間 531 ms
コンパイル使用メモリ 81,824 KB
実行使用メモリ 77,056 KB
最終ジャッジ日時 2023-10-17 03:15:57
合計ジャッジ時間 5,233 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,632 KB
testcase_01 AC 39 ms
53,632 KB
testcase_02 AC 117 ms
77,056 KB
testcase_03 AC 121 ms
77,024 KB
testcase_04 AC 121 ms
77,024 KB
testcase_05 AC 95 ms
76,372 KB
testcase_06 AC 98 ms
76,624 KB
testcase_07 AC 40 ms
53,632 KB
testcase_08 AC 109 ms
76,772 KB
testcase_09 AC 49 ms
61,396 KB
testcase_10 AC 62 ms
66,108 KB
testcase_11 AC 106 ms
76,664 KB
testcase_12 AC 69 ms
70,276 KB
testcase_13 AC 110 ms
77,032 KB
testcase_14 AC 39 ms
53,632 KB
testcase_15 AC 65 ms
68,160 KB
testcase_16 AC 104 ms
76,656 KB
testcase_17 AC 107 ms
76,776 KB
testcase_18 AC 47 ms
61,396 KB
testcase_19 AC 94 ms
76,396 KB
testcase_20 AC 108 ms
76,812 KB
testcase_21 AC 39 ms
53,632 KB
testcase_22 AC 71 ms
70,276 KB
testcase_23 AC 99 ms
76,616 KB
testcase_24 AC 68 ms
68,228 KB
testcase_25 AC 57 ms
63,724 KB
testcase_26 AC 43 ms
55,680 KB
testcase_27 AC 41 ms
53,632 KB
testcase_28 AC 40 ms
53,632 KB
testcase_29 AC 99 ms
76,652 KB
testcase_30 AC 109 ms
76,796 KB
testcase_31 AC 55 ms
63,456 KB
testcase_32 AC 101 ms
76,660 KB
testcase_33 AC 49 ms
61,392 KB
testcase_34 AC 102 ms
76,600 KB
testcase_35 AC 116 ms
77,012 KB
testcase_36 AC 98 ms
76,640 KB
testcase_37 AC 102 ms
76,628 KB
testcase_38 AC 41 ms
53,632 KB
testcase_39 AC 57 ms
63,724 KB
testcase_40 AC 46 ms
55,680 KB
testcase_41 AC 49 ms
61,392 KB
testcase_42 AC 104 ms
76,812 KB
testcase_43 AC 51 ms
61,392 KB
testcase_44 AC 41 ms
53,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heapify, heappop, heappush

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
    def find(self, x):
        """
        x が属するグループを探索して親を出す。
        """
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        """
        x と y のグループを結合
        """
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]
    def is_same(self, x, y):
        """
        x と y が同じグループか否か
        """
        return self.find(x) == self.find(y)
    def get_size(self, x):
        """
        x が属するグループの要素数
        """
        x = self.find(x)
        return self.size[x]


def main():
    N = int(input())
    G = [[-1]*N for _ in range(N)]
    for i in range(N):
        G[i][i] = 0
    M = N*(N-1)//2
    PQ = []
    for i in range(M):
        a,b,c = map(int,input().split())
        a -= 1; b -= 1
        G[a][b] = c
        G[b][a] = c
        heappush(PQ,(c,a,b))
    uf = UnionFind(N)
    ans = 0
    while PQ:
        cost,a,b = heappop(PQ)
        if uf.is_same(a,b): continue
        uf.union(a,b)
        ans = max(ans, cost)
    print(ans)
    
if __name__ == '__main__':
    main()
0