結果

問題 No.2563 色ごとのグループ
ユーザー 👑 H20H20
提出日時 2023-12-02 15:10:29
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 377 ms / 2,000 ms
コード長 1,649 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 125,312 KB
最終ジャッジ日時 2023-12-02 15:10:37
合計ジャッジ時間 6,591 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,612 KB
testcase_01 AC 38 ms
55,612 KB
testcase_02 AC 39 ms
55,612 KB
testcase_03 AC 41 ms
55,612 KB
testcase_04 AC 41 ms
55,612 KB
testcase_05 AC 40 ms
55,612 KB
testcase_06 AC 40 ms
55,612 KB
testcase_07 AC 41 ms
55,612 KB
testcase_08 AC 42 ms
55,612 KB
testcase_09 AC 44 ms
55,612 KB
testcase_10 AC 41 ms
55,612 KB
testcase_11 AC 43 ms
55,612 KB
testcase_12 AC 42 ms
55,612 KB
testcase_13 AC 42 ms
55,612 KB
testcase_14 AC 68 ms
70,888 KB
testcase_15 AC 65 ms
72,992 KB
testcase_16 AC 68 ms
72,992 KB
testcase_17 AC 68 ms
72,996 KB
testcase_18 AC 47 ms
62,060 KB
testcase_19 AC 79 ms
76,320 KB
testcase_20 AC 81 ms
77,204 KB
testcase_21 AC 78 ms
76,508 KB
testcase_22 AC 77 ms
76,432 KB
testcase_23 AC 81 ms
76,520 KB
testcase_24 AC 169 ms
93,912 KB
testcase_25 AC 142 ms
96,592 KB
testcase_26 AC 192 ms
109,680 KB
testcase_27 AC 163 ms
125,312 KB
testcase_28 AC 199 ms
114,220 KB
testcase_29 AC 220 ms
106,060 KB
testcase_30 AC 231 ms
106,060 KB
testcase_31 AC 233 ms
106,060 KB
testcase_32 AC 250 ms
106,060 KB
testcase_33 AC 351 ms
107,900 KB
testcase_34 AC 340 ms
107,332 KB
testcase_35 AC 341 ms
107,332 KB
testcase_36 AC 377 ms
107,900 KB
testcase_37 AC 341 ms
107,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict,Counter

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(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):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())


N,M = map(int, input().split())
C = list(map(int, input().split()))
S = set()
for c in C:
    S.add(c)
uf = UnionFind(N)
for i in range(M):
    x,y = map(int, input().split())
    if C[x-1]==C[y-1]:
        uf.union(x-1,y-1)
cnt = uf.group_count()
print(cnt-len(S))
0