結果
問題 | No.2563 色ごとのグループ |
ユーザー | ちーぴん |
提出日時 | 2023-12-02 15:34:00 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 377 ms / 2,000 ms |
コード長 | 1,809 bytes |
コンパイル時間 | 383 ms |
コンパイル使用メモリ | 81,828 KB |
実行使用メモリ | 111,100 KB |
最終ジャッジ日時 | 2023-12-02 15:34:07 |
合計ジャッジ時間 | 7,067 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge14 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 34 ms
53,460 KB |
testcase_01 | AC | 33 ms
53,460 KB |
testcase_02 | AC | 32 ms
53,460 KB |
testcase_03 | AC | 32 ms
53,460 KB |
testcase_04 | AC | 31 ms
53,460 KB |
testcase_05 | AC | 34 ms
53,460 KB |
testcase_06 | AC | 33 ms
53,460 KB |
testcase_07 | AC | 36 ms
53,460 KB |
testcase_08 | AC | 33 ms
53,460 KB |
testcase_09 | AC | 36 ms
53,460 KB |
testcase_10 | AC | 33 ms
53,460 KB |
testcase_11 | AC | 37 ms
53,460 KB |
testcase_12 | AC | 36 ms
53,460 KB |
testcase_13 | AC | 35 ms
53,460 KB |
testcase_14 | AC | 59 ms
70,928 KB |
testcase_15 | AC | 59 ms
70,928 KB |
testcase_16 | AC | 60 ms
70,928 KB |
testcase_17 | AC | 59 ms
70,928 KB |
testcase_18 | AC | 46 ms
62,232 KB |
testcase_19 | AC | 66 ms
73,360 KB |
testcase_20 | AC | 78 ms
76,668 KB |
testcase_21 | AC | 73 ms
76,432 KB |
testcase_22 | AC | 73 ms
76,048 KB |
testcase_23 | AC | 77 ms
76,304 KB |
testcase_24 | AC | 153 ms
88,436 KB |
testcase_25 | AC | 131 ms
90,136 KB |
testcase_26 | AC | 163 ms
99,376 KB |
testcase_27 | AC | 149 ms
110,008 KB |
testcase_28 | AC | 212 ms
102,236 KB |
testcase_29 | AC | 210 ms
110,460 KB |
testcase_30 | AC | 215 ms
110,460 KB |
testcase_31 | AC | 214 ms
110,460 KB |
testcase_32 | AC | 215 ms
110,460 KB |
testcase_33 | AC | 377 ms
110,716 KB |
testcase_34 | AC | 356 ms
111,100 KB |
testcase_35 | AC | 360 ms
110,716 KB |
testcase_36 | AC | 360 ms
110,716 KB |
testcase_37 | AC | 367 ms
110,716 KB |
ソースコード
""" 考察 色違いの場合、辺はないと考えて良さそう 色ごとのグループ数 - 1 の和が答え 色ごとの頂点数を数えておく 辺を結ぶたびに、その色の頂点数を1減らす """ # from my.union_find import UnionFind class UnionFind: def __init__(self, n: int) -> None: self.parents = [-1] * n self.data = [0] * n self.n = n def root(self, x: int) -> int: if self.parents[x] < 0: return x self.parents[x] = self.root(self.parents[x]) return self.parents[x] def unite(self, x: int, y: int) -> None: x = self.root(x) y = self.root(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x: int) -> int: return -self.parents[self.root(x)] def roots(self) -> list[int]: return [i for i, x in enumerate(self.parents) if x < 0] def same(self, x, y) -> bool: return self.root(x) == self.root(y) def groups(self) -> dict[int, list[int]]: group_members = {} for member in range(self.n): p = self.root(member) if not p in group_members: group_members[p] = [] group_members[p].append(member) return group_members N, M = map(int, input().split()) C = list(map(lambda x:int(x)-1, input().split())) cnt = [0] * N for c in C: cnt[c] += 1 uf = UnionFind(N) for _ in range(M): a, b = map(int, input().split()) a -= 1; b -= 1 if C[a] == C[b]: if uf.same(a, b): continue else: uf.unite(a, b) cnt[C[a]] -= 1 ans = sum(c - 1 for c in cnt if c > 0) print(ans)