結果

問題 No.2563 色ごとのグループ
ユーザー CecilCecil
提出日時 2023-12-02 16:49:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,509 ms / 2,000 ms
コード長 2,617 bytes
コンパイル時間 414 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 91,068 KB
最終ジャッジ日時 2023-12-02 16:49:53
合計ジャッジ時間 19,593 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,240 KB
testcase_01 AC 30 ms
10,240 KB
testcase_02 AC 30 ms
10,240 KB
testcase_03 AC 31 ms
10,240 KB
testcase_04 AC 30 ms
10,240 KB
testcase_05 AC 31 ms
10,240 KB
testcase_06 AC 30 ms
10,240 KB
testcase_07 AC 30 ms
10,240 KB
testcase_08 AC 31 ms
10,240 KB
testcase_09 AC 31 ms
10,240 KB
testcase_10 AC 30 ms
10,240 KB
testcase_11 AC 32 ms
10,240 KB
testcase_12 AC 31 ms
10,240 KB
testcase_13 AC 31 ms
10,240 KB
testcase_14 AC 37 ms
10,752 KB
testcase_15 AC 38 ms
10,752 KB
testcase_16 AC 38 ms
10,624 KB
testcase_17 AC 38 ms
10,624 KB
testcase_18 AC 38 ms
10,880 KB
testcase_19 AC 53 ms
10,752 KB
testcase_20 AC 102 ms
16,512 KB
testcase_21 AC 73 ms
13,312 KB
testcase_22 AC 64 ms
12,032 KB
testcase_23 AC 97 ms
12,800 KB
testcase_24 AC 736 ms
43,260 KB
testcase_25 AC 679 ms
47,264 KB
testcase_26 AC 919 ms
62,076 KB
testcase_27 AC 997 ms
87,536 KB
testcase_28 AC 1,139 ms
74,748 KB
testcase_29 AC 1,475 ms
91,064 KB
testcase_30 AC 1,509 ms
91,068 KB
testcase_31 AC 1,490 ms
90,928 KB
testcase_32 AC 1,507 ms
90,928 KB
testcase_33 AC 1,212 ms
35,856 KB
testcase_34 AC 1,211 ms
35,612 KB
testcase_35 AC 1,203 ms
35,612 KB
testcase_36 AC 1,213 ms
35,856 KB
testcase_37 AC 1,219 ms
35,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict as dd

class UnionFind():
    #初期化
    def __init__(self, n):
        self.par = [-1] * n
        self.rank = [0] * n
        self.siz = [1] * n
    #根を求める
    def root(self, x):
        if self.par[x] == -1:
            return x#xが根の場合はxを返す
        else:
            self.par[x] = self.root(self.par[x]) #経路圧縮
            return self.par[x]
    #xとyが同じグループに属するか(根が一致するか)
    def issame(self, x, y):
        return self.root(x) == self.root(y)
    #xを含むグループとyを含むグループを併合する
    def unite(self, x, y):
        #x側とy側の根を取得する
        rx = self.root(x)
        ry = self.root(y)
        if rx == ry:
            return False #すでに同じグループの時は何もしない
        #union by rank
        if self.rank[rx] < self.rank[ry]: #ry側のrankが小さくなるようにする
            rx, ry = ry, rx
        self.par[ry] = rx #ryをrxの子とする
        if self.rank[rx] == self.rank[ry]:#ry側のrankを調整する
            self.rank[rx] += 1
        self.rank[rx] += self.siz[ry] #rx側のsizを調整する
        return True
    #xを含む根付き木のサイズを求める
    def size(self, x):
        return self.siz[self.root(x)]
      
N,M = map(int, input().split())
C = list(map(int, input().split()))

uf = UnionFind(N+1)
col = dd(list)
col_root_set = dd(set)
for i in range(N):
    col[C[i]].append(i+1)
for _ in range(M):
    u,v = map(int,input().split())
    if C[u-1] != C[v-1]:
        continue
    uf.unite(u,v)
    #print("#",uf.root(u),uf.root(v))
    #col_root_set[C[u-1]].add(uf.root(u))
    #col_root_set[C[v-1]].add(uf.root(v))
    """if not col_root[C[u]]:
        col_root[C[u]] = [uf.root(u),u]
    if uf.size(u) > uf.size(col_root[C[u]][0]):
        col_root[C[u]] = [uf.root(u),u]"""
ans = 0
#print(col_root)
for i in range(1, N+1):
    col_root_set[C[i-1]].add(uf.root(i))
#print(col_root_set)
for key in col.keys():
    #print(col_root_set[key])
    ans += max(len(col_root_set[key])-1,0)
    """if col_root[key]:
        c,u = col_root[key][0],col_root[key][1]
    else:
        c,u = col[key][0],col[key][0]
    for i in range(n):
        if c != uf.root(col[key][i]):
            uf.unite(u,col[key][i])
            ans += 1"""
    """for i in range(n):
        for j in range(i, n):
            if i == j:
                continue
            if not uf.issame(col[key][i],col[key][j]):
                uf.unite(col[key][i],col[key][j])
                ans += 1"""
print(ans)
0