結果
問題 | No.3087 University Coloring |
ユーザー |
|
提出日時 | 2025-04-04 23:18:22 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,091 ms / 2,000 ms |
コード長 | 1,040 bytes |
コンパイル時間 | 339 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 110,440 KB |
最終ジャッジ日時 | 2025-04-04 23:19:01 |
合計ジャッジ時間 | 26,137 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
class UnionFind: def __init__(self, n): self.parent = list(range(n + 1)) self.rank = [0] * (n + 1) def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): root_x = self.find(x) root_y = self.find(y) if root_x != root_y: if self.rank[root_x] > self.rank[root_y]: self.parent[root_y] = root_x elif self.rank[root_x] < self.rank[root_y]: self.parent[root_x] = root_y else: self.parent[root_y] = root_x self.rank[root_x] += 1 def same(self, x, y): return self.find(x) == self.find(y) N, M = map(int, input().split()) edges = [] for _ in range(M): a, b, c = map(int, input().split()) edges.append((c, a, b)) edges.sort(reverse=True) uf = UnionFind(N) ans = 0 for c, a, b in edges: if not uf.same(a, b): uf.union(a, b) ans += c print(ans*2)