結果

問題 No.2563 色ごとのグループ
ユーザー かもめのうでかもめのうで
提出日時 2023-12-02 19:57:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 467 ms / 2,000 ms
コード長 2,180 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 112,244 KB
最終ジャッジ日時 2023-12-02 19:57:10
合計ジャッジ時間 8,385 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,480 KB
testcase_01 AC 42 ms
55,480 KB
testcase_02 AC 41 ms
55,480 KB
testcase_03 AC 42 ms
55,480 KB
testcase_04 AC 43 ms
55,480 KB
testcase_05 AC 42 ms
55,480 KB
testcase_06 AC 43 ms
55,480 KB
testcase_07 AC 43 ms
55,480 KB
testcase_08 AC 42 ms
55,480 KB
testcase_09 AC 44 ms
55,480 KB
testcase_10 AC 43 ms
55,480 KB
testcase_11 AC 44 ms
55,480 KB
testcase_12 AC 43 ms
55,480 KB
testcase_13 AC 43 ms
55,480 KB
testcase_14 AC 74 ms
73,132 KB
testcase_15 AC 72 ms
73,104 KB
testcase_16 AC 73 ms
73,104 KB
testcase_17 AC 74 ms
73,116 KB
testcase_18 AC 57 ms
64,432 KB
testcase_19 AC 82 ms
76,556 KB
testcase_20 AC 88 ms
77,552 KB
testcase_21 AC 88 ms
76,792 KB
testcase_22 AC 85 ms
76,556 KB
testcase_23 AC 90 ms
76,812 KB
testcase_24 AC 181 ms
88,916 KB
testcase_25 AC 172 ms
90,952 KB
testcase_26 AC 208 ms
99,912 KB
testcase_27 AC 210 ms
111,224 KB
testcase_28 AC 232 ms
103,276 KB
testcase_29 AC 279 ms
111,812 KB
testcase_30 AC 277 ms
111,812 KB
testcase_31 AC 275 ms
111,812 KB
testcase_32 AC 296 ms
111,812 KB
testcase_33 AC 428 ms
112,244 KB
testcase_34 AC 428 ms
111,548 KB
testcase_35 AC 439 ms
111,548 KB
testcase_36 AC 467 ms
112,244 KB
testcase_37 AC 439 ms
112,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
class UnionFind():
    def __init__(self, n):
        self.par = [-1]*n#頂点xの親を記録
        self.rank = [0]*n#頂点xが属する根付き木の高さを記録
        self.siz = [1]*n#その頂点数を記録
        self.edgecount = [0]*n#頂点xの親の属する根付き木の辺の数

    #xの根を求める
    def root(self, x):
        # while True:
        #     if self.par[x] == -1:
        #         break
        #     x = self.par[x]
        # return x
        #経路圧縮
        if self.par[x] == -1:
            return x
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]
        
    #xとyの統合
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry:
            self.edgecount[rx] += 1
            return False
        #union by size
        if self.siz[rx] < self.siz[ry]:
            self.par[rx] = ry
            self.siz[ry] += self.siz[rx]
            self.edgecount[ry] += 1 + self.edgecount[rx]
        else:
            self.par[ry] = rx
            self.siz[rx] += self.siz[ry]
            self.edgecount[rx] += 1 + self.edgecount[ry]
        return True
    
    #xとyが同じグループに属すか
    def issame(self, x, y):
        if self.root(x) == self.root(y):
            return True
        return False
    
    #xが属すグループの要素数を返す
    def size(self, x):
        return self.siz[self.root(x)]

    #頂点xの親の属する根付き木の辺の数を返す
    def edge(self, x):
        return self.edgecount[self.root(x)]
    
    #連結成分の個数を返す
    def groupcount(self, x):
        dic = defaultdict(int)
        for i in range(x):
            dic[self.root(i)] += 1
        return len(dic)
    
n, m = map(int, input().split())
c = list(map(int, input().split()))
uf = UnionFind(n)
for _ in range(m):
    v, u = map(int, input().split())
    v -= 1
    u -= 1
    if c[v] == c[u]:
        uf.unite(v, u)
d = defaultdict(int)
for i in range(n):
    if i == uf.root(i):
        d[c[i]] += 1
ans = 0
for i, j in d.items():
    ans += j-1
print(ans)
0