結果

問題 No.2563 色ごとのグループ
ユーザー nzm_ortnzm_ort
提出日時 2023-12-02 15:33:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 2,304 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 135,024 KB
最終ジャッジ日時 2023-12-02 15:33:44
合計ジャッジ時間 7,718 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,612 KB
testcase_01 AC 38 ms
55,612 KB
testcase_02 AC 38 ms
55,612 KB
testcase_03 AC 38 ms
55,612 KB
testcase_04 AC 38 ms
55,612 KB
testcase_05 AC 38 ms
55,612 KB
testcase_06 AC 42 ms
55,612 KB
testcase_07 AC 42 ms
55,612 KB
testcase_08 AC 40 ms
55,612 KB
testcase_09 AC 43 ms
55,612 KB
testcase_10 AC 40 ms
55,612 KB
testcase_11 AC 42 ms
55,612 KB
testcase_12 AC 42 ms
55,612 KB
testcase_13 AC 41 ms
55,612 KB
testcase_14 AC 74 ms
73,988 KB
testcase_15 AC 74 ms
74,372 KB
testcase_16 AC 75 ms
74,116 KB
testcase_17 AC 74 ms
73,988 KB
testcase_18 AC 53 ms
66,664 KB
testcase_19 AC 80 ms
76,420 KB
testcase_20 AC 89 ms
78,788 KB
testcase_21 AC 91 ms
76,876 KB
testcase_22 AC 92 ms
76,644 KB
testcase_23 AC 98 ms
76,636 KB
testcase_24 AC 196 ms
97,644 KB
testcase_25 AC 179 ms
100,492 KB
testcase_26 AC 228 ms
114,448 KB
testcase_27 AC 233 ms
135,024 KB
testcase_28 AC 246 ms
118,052 KB
testcase_29 AC 307 ms
132,884 KB
testcase_30 AC 319 ms
132,884 KB
testcase_31 AC 307 ms
132,884 KB
testcase_32 AC 305 ms
132,884 KB
testcase_33 AC 382 ms
107,644 KB
testcase_34 AC 401 ms
107,076 KB
testcase_35 AC 388 ms
107,076 KB
testcase_36 AC 384 ms
107,644 KB
testcase_37 AC 405 ms
107,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
    # 要素xが属するグループの根(親)を返す
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    # 要素xと要素yを結合
    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
    # 要素xが属するグループの要素数を返す
    def size(self, x):
        return -self.parents[self.find(x)]
    # 要素xと要素yが同じグループに属するかを返す
    def same(self, x, y):
        return self.find(x) == self.find(y)
    # 要素xが属するグループのメンバーをリストで返す
    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()))
uf = UnionFind(n)
for i in range(m):
    u, v = map(lambda x: x-1, map(int, input().split()))
    if c[u]!=c[v]: continue
    uf.union(u, v)

group = uf.all_group_members()
d = defaultdict(int)
for key in list(group.keys()):
    d[c[key]] += 1

ans = 0
for key in list(d.keys()):
    ans += d[key]-1
print(ans)
0