結果
問題 | No.2418 情報通だよ!Nafmoくん |
ユーザー | stunniita |
提出日時 | 2023-08-12 14:46:10 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 465 ms / 2,000 ms |
コード長 | 1,833 bytes |
コンパイル時間 | 240 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 16,640 KB |
最終ジャッジ日時 | 2024-04-30 07:08:30 |
合計ジャッジ時間 | 6,948 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 33 ms
10,880 KB |
testcase_01 | AC | 33 ms
10,880 KB |
testcase_02 | AC | 31 ms
10,880 KB |
testcase_03 | AC | 425 ms
16,512 KB |
testcase_04 | AC | 94 ms
14,464 KB |
testcase_05 | AC | 308 ms
13,056 KB |
testcase_06 | AC | 281 ms
15,872 KB |
testcase_07 | AC | 327 ms
12,544 KB |
testcase_08 | AC | 286 ms
16,640 KB |
testcase_09 | AC | 399 ms
13,696 KB |
testcase_10 | AC | 424 ms
16,000 KB |
testcase_11 | AC | 371 ms
15,488 KB |
testcase_12 | AC | 258 ms
14,592 KB |
testcase_13 | AC | 139 ms
12,160 KB |
testcase_14 | AC | 238 ms
13,440 KB |
testcase_15 | AC | 305 ms
10,880 KB |
testcase_16 | AC | 465 ms
14,848 KB |
testcase_17 | AC | 159 ms
11,008 KB |
testcase_18 | AC | 227 ms
15,104 KB |
testcase_19 | AC | 75 ms
14,080 KB |
testcase_20 | AC | 333 ms
11,008 KB |
testcase_21 | AC | 165 ms
12,800 KB |
testcase_22 | AC | 186 ms
11,520 KB |
ソースコード
# Base code by algo-method (URL-> ttps://algo-method.com/descriptions/133) class UnionFind(): # 初期化 def __init__(self, n:int): self.par = [-1] * n self.rank = [0] * n self.siz = [1] * n self.len = n # 根を求める def root(self, x:int) -> int: if self.par[x] == -1: return x # x が根の場合は x を返す else: self.par[x] = self.root(self.par[x]) # 経路圧縮 return self.par[x] # x と y が同じグループに属するか (根が一致するか) def issame(self, x:int, y:int) -> bool: return self.root(x) == self.root(y) # x を含むグループと y を含むグループを併合する def unite(self, x:int, y:int) -> bool: # x 側と y 側の根を取得する rx = self.root(x) ry = self.root(y) if rx == ry: return False # すでに同じグループのときは何もしない # union by rank if self.rank[rx] < self.rank[ry]: # ry 側の rank が小さくなるようにする rx, ry = ry, rx self.par[ry] = rx # ry を rx の子とする if self.rank[rx] == self.rank[ry]: # rx 側の rank を調整する self.rank[rx] += 1 self.siz[rx] += self.siz[ry] # rx 側の siz を調整する return True # x を含む根付き木のサイズを求める def size(self, x:int) -> int: return self.siz[self.root(x)] def solve(self) -> int: x = 0 for i in range(self.len): if self.par[i] == -1: x += self.siz[i] // 2 return x N,M = map(int, input().split()) UF = UnionFind(2*N) for i in range(M): a,b = map(int, input().split()) UF.unite(a-1,b-1) #print(vars(UF)) x = UF.solve() print(N-x)