結果

問題 No.2563 色ごとのグループ
ユーザー lloyzlloyz
提出日時 2023-12-10 01:11:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 484 ms / 2,000 ms
コード長 2,373 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 121,204 KB
最終ジャッジ日時 2023-12-10 01:11:33
合計ジャッジ時間 9,971 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,608 KB
testcase_01 AC 38 ms
55,608 KB
testcase_02 AC 38 ms
55,608 KB
testcase_03 AC 38 ms
55,608 KB
testcase_04 AC 38 ms
55,608 KB
testcase_05 AC 39 ms
55,608 KB
testcase_06 AC 38 ms
55,608 KB
testcase_07 AC 37 ms
55,608 KB
testcase_08 AC 37 ms
55,608 KB
testcase_09 AC 40 ms
55,608 KB
testcase_10 AC 39 ms
55,608 KB
testcase_11 AC 42 ms
55,608 KB
testcase_12 AC 44 ms
55,608 KB
testcase_13 AC 46 ms
55,608 KB
testcase_14 AC 79 ms
72,984 KB
testcase_15 AC 72 ms
72,980 KB
testcase_16 AC 71 ms
72,984 KB
testcase_17 AC 69 ms
72,984 KB
testcase_18 AC 51 ms
64,304 KB
testcase_19 AC 96 ms
76,820 KB
testcase_20 AC 108 ms
80,856 KB
testcase_21 AC 93 ms
78,020 KB
testcase_22 AC 103 ms
77,528 KB
testcase_23 AC 89 ms
77,648 KB
testcase_24 AC 238 ms
93,992 KB
testcase_25 AC 188 ms
95,888 KB
testcase_26 AC 246 ms
106,224 KB
testcase_27 AC 256 ms
116,188 KB
testcase_28 AC 262 ms
108,776 KB
testcase_29 AC 350 ms
118,092 KB
testcase_30 AC 347 ms
118,992 KB
testcase_31 AC 340 ms
118,356 KB
testcase_32 AC 357 ms
118,988 KB
testcase_33 AC 456 ms
120,024 KB
testcase_34 AC 457 ms
121,204 KB
testcase_35 AC 484 ms
120,432 KB
testcase_36 AC 456 ms
120,028 KB
testcase_37 AC 455 ms
120,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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()))
P = [set() for _ in range(n)]
for i in range(n):
    P[C[i] - 1].add(i)
UF = UnionFind(n)
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if C[u] != C[v]:
        continue
    c = C[u] - 1
    uroot = UF.find(u)
    vroot = UF.find(v)
    if uroot == vroot:
        continue
    UF.union(u, v)
    root = UF.find(u)
    if root == uroot:
        P[c].remove(vroot)
    else:
        P[c].remove(uroot)
ans = 0
for i in range(n):
    ans += max(len(P[i]) - 1, 0)
print(ans)
0