結果

問題 No.2563 色ごとのグループ
ユーザー YuuuTYuuuT
提出日時 2023-12-02 15:17:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 2,866 bytes
コンパイル時間 550 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 116,084 KB
最終ジャッジ日時 2023-12-02 15:17:07
合計ジャッジ時間 6,527 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
55,764 KB
testcase_01 AC 38 ms
55,764 KB
testcase_02 AC 38 ms
55,764 KB
testcase_03 AC 37 ms
55,764 KB
testcase_04 AC 38 ms
55,764 KB
testcase_05 AC 38 ms
55,764 KB
testcase_06 AC 46 ms
55,764 KB
testcase_07 AC 39 ms
55,764 KB
testcase_08 AC 39 ms
55,764 KB
testcase_09 AC 38 ms
55,764 KB
testcase_10 AC 39 ms
55,764 KB
testcase_11 AC 39 ms
55,764 KB
testcase_12 AC 38 ms
55,764 KB
testcase_13 AC 37 ms
55,764 KB
testcase_14 AC 53 ms
66,756 KB
testcase_15 AC 52 ms
68,980 KB
testcase_16 AC 51 ms
66,760 KB
testcase_17 AC 50 ms
66,760 KB
testcase_18 AC 47 ms
64,580 KB
testcase_19 AC 54 ms
71,028 KB
testcase_20 AC 66 ms
78,532 KB
testcase_21 AC 58 ms
74,620 KB
testcase_22 AC 60 ms
73,212 KB
testcase_23 AC 66 ms
77,060 KB
testcase_24 AC 142 ms
98,800 KB
testcase_25 AC 129 ms
99,276 KB
testcase_26 AC 159 ms
104,188 KB
testcase_27 AC 175 ms
115,004 KB
testcase_28 AC 174 ms
106,264 KB
testcase_29 AC 237 ms
116,084 KB
testcase_30 AC 223 ms
115,952 KB
testcase_31 AC 234 ms
115,916 KB
testcase_32 AC 211 ms
115,908 KB
testcase_33 AC 332 ms
108,104 KB
testcase_34 AC 318 ms
107,616 KB
testcase_35 AC 349 ms
107,616 KB
testcase_36 AC 321 ms
108,104 KB
testcase_37 AC 323 ms
108,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,math
#if len(sys.argv)==2:sys.stdin=open(sys.argv[1])
from collections import defaultdict,deque
from itertools import combinations,permutations,accumulate,product
from bisect import bisect,bisect_left,bisect_right
from heapq import heappop,heappush,heapify
#from sortedcontainers import SortedList,SortedSet
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def ms(): return map(int, input().split())
def li(): return list(map(int,input().split()))
class UnionFind(): # n頂点のUnion-Find木を作成
    def __init__(self, n):
        self.n = n
				#各頂点の親(1個上の要素)を記入
        self.parents = [-1] * (n+1)  # その頂点が根なら-1(unionを実行すると-1以下の値になることもある)

		# 頂点xの根を返す関数(経路圧縮)
    def root(self, x):
        if self.parents[x] < 0: return x # xが根ならxを返す
        else:
            self.parents[x] = self.root(self.parents[x])
            return self.parents[x]

		# 要素x,yを統合する関数
    def union(self, x, y):
        x = self.root(x)
        y = self.root(y)
				# x,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.root(x)]

		# x,yが同じグループか(連結か)を返す関数
    def same(self, x, y): return self.root(x) == self.root(y)

		# xが属するグループの要素をリストで返す
    def members(self, x):
        root = self.root(x)
        return [i for i in range(self.n) if self.root(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 = ms()
uf = UnionFind(N)
C = li()
ans = 0
gro = defaultdict(list)
for i in range(N):
  gro[C[i]].append(i+1)
cnt = defaultdict(int)
for _ in range(M):
  u,v = ms()
  if C[u-1]==C[v-1]:
    if uf.same(u,v): continue
    uf.union(u,v)
    cnt[C[u-1]] += 1
for c in gro.keys():
  ans += max(len(gro[c])-cnt[c]-1,0)
print(ans) 
0