結果
問題 | No.2418 情報通だよ!Nafmoくん |
ユーザー | NakLon131 |
提出日時 | 2023-08-12 15:47:02 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 253 ms / 2,000 ms |
コード長 | 1,409 bytes |
コンパイル時間 | 219 ms |
コンパイル使用メモリ | 82,040 KB |
実行使用メモリ | 78,204 KB |
最終ジャッジ日時 | 2024-04-30 10:13:03 |
合計ジャッジ時間 | 4,779 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
54,000 KB |
testcase_01 | AC | 41 ms
53,204 KB |
testcase_02 | AC | 41 ms
53,268 KB |
testcase_03 | AC | 193 ms
77,920 KB |
testcase_04 | AC | 95 ms
77,644 KB |
testcase_05 | AC | 220 ms
77,708 KB |
testcase_06 | AC | 153 ms
77,816 KB |
testcase_07 | AC | 204 ms
77,436 KB |
testcase_08 | AC | 143 ms
78,204 KB |
testcase_09 | AC | 226 ms
78,108 KB |
testcase_10 | AC | 199 ms
77,848 KB |
testcase_11 | AC | 179 ms
78,100 KB |
testcase_12 | AC | 148 ms
77,504 KB |
testcase_13 | AC | 147 ms
77,432 KB |
testcase_14 | AC | 164 ms
77,392 KB |
testcase_15 | AC | 118 ms
76,200 KB |
testcase_16 | AC | 253 ms
78,056 KB |
testcase_17 | AC | 135 ms
76,992 KB |
testcase_18 | AC | 149 ms
77,668 KB |
testcase_19 | AC | 88 ms
77,156 KB |
testcase_20 | AC | 134 ms
76,400 KB |
testcase_21 | AC | 138 ms
77,032 KB |
testcase_22 | AC | 168 ms
77,248 KB |
ソースコード
class UnionFind: # 初期化(nは要素数) def __init__(self, n) -> None: # 全体の大きさ self.n = n # グループのサイズ(自身が根) self.root_sz = [-1] * n # 要素の根を返す def root(self, a) -> int: # aが根ならa自身を返す if self.root_sz[a] < 0: return a # aが根でない場合は、親を辿る self.root_sz[a] = self.root(self.root_sz[a]) return self.root_sz[a] # aとbを結合(サイズ更新) # ※マージ可否、マージ後の親を返すなど実装に応じて変える def merge(self, a, b) -> bool: # a,bの根を調べ、同じなら終了 a, b = self.root(a), self.root(b) if a == b: return False # グループのサイズ=要素数を比較 サイズの小さい方を大きい方につなげるため if(abs(self.root_sz[a]) < abs(self.root_sz[b])): a, b = b, a # サイズの更新 self.root_sz[a] += self.root_sz[b] self.root_sz[b] = a return True # 同じグループか判定 def same(self, a, b) -> bool: return (self.root(a) == self.root(b)) # 属するグループのサイズを返す def size(self, a) -> int: return abs(self.root_sz[self.root(a)]) n, m = map(int, input().split()) uf = UnionFind(2*n) for i in range(m): a, b = map(lambda x: int(x)-1, input().split()) uf.merge(a, b) ans = 0 for i in range(2*n): if i == uf.root(i): ans += uf.size(i) // 2 print(n - ans)