結果

問題 No.2563 色ごとのグループ
ユーザー oginoshikibuoginoshikibu
提出日時 2023-12-02 15:22:27
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,673 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 284,684 KB
最終ジャッジ日時 2023-12-02 15:22:36
合計ジャッジ時間 7,202 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
74,528 KB
testcase_01 AC 60 ms
67,720 KB
testcase_02 AC 60 ms
67,720 KB
testcase_03 AC 60 ms
67,720 KB
testcase_04 AC 60 ms
67,720 KB
testcase_05 AC 60 ms
67,724 KB
testcase_06 AC 60 ms
67,720 KB
testcase_07 AC 61 ms
67,720 KB
testcase_08 AC 60 ms
67,720 KB
testcase_09 AC 62 ms
67,724 KB
testcase_10 AC 58 ms
67,724 KB
testcase_11 AC 61 ms
67,720 KB
testcase_12 AC 63 ms
67,720 KB
testcase_13 AC 64 ms
67,724 KB
testcase_14 AC 103 ms
78,252 KB
testcase_15 AC 102 ms
78,388 KB
testcase_16 AC 99 ms
78,252 KB
testcase_17 AC 100 ms
78,252 KB
testcase_18 AC 87 ms
78,268 KB
testcase_19 AC 105 ms
78,388 KB
testcase_20 AC 488 ms
260,144 KB
testcase_21 AC 182 ms
82,356 KB
testcase_22 AC 136 ms
79,676 KB
testcase_23 AC 163 ms
81,212 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import types

_atcoder_code = """
# Python port of AtCoder Library.

__version__ = '0.0.1'
"""

atcoder = types.ModuleType('atcoder')
exec(_atcoder_code, atcoder.__dict__)

_atcoder_dsu_code = """
import typing


class DSU:
    '''
    Implement (union by size) + (path halving)

    Reference:
    Zvi Galil and Giuseppe F. Italiano,
    Data structures and algorithms for disjoint set union problems
    '''

    def __init__(self, n: int = 0) -> None:
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a: int, b: int) -> int:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        x = self.leader(a)
        y = self.leader(b)

        if x == y:
            return x

        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x

        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x

        return x

    def same(self, a: int, b: int) -> bool:
        assert 0 <= a < self._n
        assert 0 <= b < self._n

        return self.leader(a) == self.leader(b)

    def leader(self, a: int) -> int:
        assert 0 <= a < self._n

        parent = self.parent_or_size[a]
        while parent >= 0:
            if self.parent_or_size[parent] < 0:
                return parent
            self.parent_or_size[a], a, parent = (
                self.parent_or_size[parent],
                self.parent_or_size[parent],
                self.parent_or_size[self.parent_or_size[parent]]
            )

        return a

    def size(self, a: int) -> int:
        assert 0 <= a < self._n

        return -self.parent_or_size[self.leader(a)]

    def groups(self) -> typing.List[typing.List[int]]:
        leader_buf = [self.leader(i) for i in range(self._n)]

        result: typing.List[typing.List[int]] = [[] for _ in range(self._n)]
        for i in range(self._n):
            result[leader_buf[i]].append(i)

        return list(filter(lambda r: r, result))
"""

atcoder.dsu = types.ModuleType('atcoder.dsu')
exec(_atcoder_dsu_code, atcoder.dsu.__dict__)
DSU = atcoder.dsu.DSU

from collections import defaultdict
# from atcoder.dsu import DSU
n,m=map(int, input().split())
C=list(map(int, input().split()))
Cnode=defaultdict(set)
for i,c in enumerate(C):
    Cnode[c].add(i)

Cedge=defaultdict(set)

for _ in range(m):
    u,v=map(lambda x: int(x) - 1, input().split())
    if C[u] == C[v]:
        Cedge[C[u]].add((u,v))
ans=0

for c in Cnode.keys():
    dsu=DSU(n)
    for u,v in Cedge[c]:
        dsu.merge(u,v)
    node1=Cnode[c].pop()
    for u in Cnode[c]:
        if not dsu.same(u,node1):
            dsu.merge(u,node1)
            ans+=1

print(ans)
0