結果

問題 No.2563 色ごとのグループ
ユーザー KemtyKemty
提出日時 2023-12-02 15:02:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 346 ms / 2,000 ms
コード長 2,611 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 133,840 KB
最終ジャッジ日時 2023-12-02 15:02:21
合計ジャッジ時間 7,155 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
79,952 KB
testcase_01 AC 85 ms
79,952 KB
testcase_02 AC 81 ms
79,952 KB
testcase_03 AC 82 ms
79,952 KB
testcase_04 AC 94 ms
79,952 KB
testcase_05 AC 84 ms
79,952 KB
testcase_06 AC 81 ms
79,952 KB
testcase_07 AC 79 ms
79,952 KB
testcase_08 AC 81 ms
79,952 KB
testcase_09 AC 82 ms
79,952 KB
testcase_10 AC 84 ms
79,952 KB
testcase_11 AC 82 ms
79,952 KB
testcase_12 AC 84 ms
79,952 KB
testcase_13 AC 79 ms
79,952 KB
testcase_14 AC 89 ms
81,104 KB
testcase_15 AC 88 ms
81,104 KB
testcase_16 AC 105 ms
81,104 KB
testcase_17 AC 90 ms
81,104 KB
testcase_18 AC 86 ms
80,848 KB
testcase_19 AC 91 ms
81,104 KB
testcase_20 AC 97 ms
82,000 KB
testcase_21 AC 92 ms
81,104 KB
testcase_22 AC 91 ms
81,104 KB
testcase_23 AC 94 ms
81,104 KB
testcase_24 AC 153 ms
99,920 KB
testcase_25 AC 146 ms
102,608 KB
testcase_26 AC 197 ms
116,816 KB
testcase_27 AC 185 ms
133,072 KB
testcase_28 AC 191 ms
121,552 KB
testcase_29 AC 249 ms
133,840 KB
testcase_30 AC 222 ms
133,584 KB
testcase_31 AC 217 ms
133,584 KB
testcase_32 AC 226 ms
133,584 KB
testcase_33 AC 313 ms
113,616 KB
testcase_34 AC 321 ms
113,104 KB
testcase_35 AC 316 ms
113,104 KB
testcase_36 AC 346 ms
113,616 KB
testcase_37 AC 319 ms
113,616 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque, Counter
import copy
from itertools import combinations, permutations, product, accumulate, groupby, chain
from heapq import heapify, heappop, heappush
import math
import bisect
from pprint import pprint
from random import randint
import sys
# sys.setrecursionlimit(200000)
input = lambda: sys.stdin.readline().rstrip('\n')
inf = float('inf')
mod1 = 10**9+7
mod2 = 998244353
def ceil_div(x, y): return -(-x//y)

#################################################    

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の属する集合同士を連結(要素数が少ない方を多い方に連結)
    #辺を追加したらTrue, しなければFalseを返す
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        return True

    #点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 get_groups(self):
        groups = defaultdict(list)
        for x in range(self.n):
            groups[self.find(x)].append(x)
        return groups

    #print(インスタンス)で、全集合の「根と全要素」を出力
    def __str__(self):
        return '\n'.join('{}:{}'.format(r, self.menbers(r)) for r in self.roots())

N, M = map(int, input().split())
C = list(map(int, input().split()))
cnt = Counter(C)
s = 0
for v in cnt.values():
    s += v-1
uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    if C[a] == C[b]:
        s -= uf.union(a, b)
print(s)
0