結果
| 問題 |
No.1639 最小通信路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-06-27 22:58:00 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 163 ms / 2,000 ms |
| コード長 | 1,795 bytes |
| コンパイル時間 | 200 ms |
| コンパイル使用メモリ | 82,176 KB |
| 実行使用メモリ | 78,720 KB |
| 最終ジャッジ日時 | 2024-11-20 06:35:12 |
| 合計ジャッジ時間 | 6,351 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 43 |
ソースコード
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)