結果
問題 |
No.556 仁義なきサルたち
|
ユーザー |
![]() |
提出日時 | 2020-02-28 14:31:39 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 47 ms / 2,000 ms |
コード長 | 1,052 bytes |
コンパイル時間 | 123 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 12,288 KB |
最終ジャッジ日時 | 2024-10-13 16:24:44 |
合計ジャッジ時間 | 1,844 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#!/usr/bin/env python3 # %% import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines # %% N, M = map(int, readline().split()) m = map(int, read().split()) AB = zip(m, m) # %% class UnionFind: def __init__(self, N): self.root = list(range(N)) self.size = [1] * (N) def find_root(self, x): root = self.root while root[x] != x: root[x] = root[root[x]] x = root[x] return x def merge(self, x, y): x = self.find_root(x) y = self.find_root(y) if x == y: return False if x > y: x, y = y, x sx, sy = self.size[x], self.size[y] if sx < sy: self.root[x] = y self.size[y] += sx else: self.root[y] = x self.size[x] += sy return True # %% uf = UnionFind(N + 1) for a, b in AB: uf.merge(a, b) # %% boss = (uf.find_root(x) for x in range(1, N + 1)) print('\n'.join(map(str, boss)))