結果
| 問題 |
No.1390 Get together
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-01-27 17:25:16 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 389 ms / 2,000 ms |
| コード長 | 1,139 bytes |
| コンパイル時間 | 353 ms |
| コンパイル使用メモリ | 82,848 KB |
| 実行使用メモリ | 98,944 KB |
| 最終ジャッジ日時 | 2024-09-28 09:44:25 |
| 合計ジャッジ時間 | 7,974 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
class UnionFind:
def __init__(self, n):
self.parent = [-1] * n
self.size = [1] * n
def find(self, x):
if self.parent[x] == -1: return x
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
px = self.find(x)
py = self.find(y)
if px == py: return
if self.size[px] < self.size[py]: px, py = py, px
self.parent[py] = px
self.size[px] += self.size[py]
def same(self, x, y):
return self.find(x) == self.find(y)
def get_size(self, x):
return self.size[self.find(x)]
def get_all_groups(self):
n = len(self.parent)
groups = [[] for _ in range(n)]
for i in range(n):
groups[self.find(i)].append(i)
return list(filter(lambda x: x, groups))
N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
for _ in range(N):
b, c = map(int, input().split())
G[c].append(b)
uf = UnionFind(M+1)
ans = 0
for col in G:
for box in col:
if not uf.same(col[0], box):
uf.union(col[0], box)
ans += 1
print(ans)