結果

問題 No.2563 色ごとのグループ
ユーザー CecilCecil
提出日時 2023-12-02 16:48:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,615 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 90,968 KB
最終ジャッジ日時 2023-12-02 16:48:38
合計ジャッジ時間 20,375 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,240 KB
testcase_01 AC 33 ms
10,240 KB
testcase_02 AC 29 ms
10,240 KB
testcase_03 AC 29 ms
10,240 KB
testcase_04 AC 32 ms
10,240 KB
testcase_05 AC 30 ms
10,240 KB
testcase_06 AC 30 ms
10,240 KB
testcase_07 AC 30 ms
10,240 KB
testcase_08 AC 29 ms
10,240 KB
testcase_09 AC 30 ms
10,240 KB
testcase_10 AC 30 ms
10,240 KB
testcase_11 AC 30 ms
10,240 KB
testcase_12 AC 31 ms
10,240 KB
testcase_13 AC 30 ms
10,240 KB
testcase_14 AC 36 ms
10,752 KB
testcase_15 AC 39 ms
10,752 KB
testcase_16 AC 38 ms
10,752 KB
testcase_17 AC 36 ms
10,624 KB
testcase_18 AC 34 ms
10,880 KB
testcase_19 AC 54 ms
10,752 KB
testcase_20 AC 99 ms
16,384 KB
testcase_21 AC 70 ms
13,184 KB
testcase_22 AC 62 ms
11,904 KB
testcase_23 AC 91 ms
12,672 KB
testcase_24 AC 772 ms
42,956 KB
testcase_25 AC 670 ms
47,208 KB
testcase_26 AC 881 ms
61,980 KB
testcase_27 AC 929 ms
87,380 KB
testcase_28 AC 1,085 ms
74,652 KB
testcase_29 AC 1,405 ms
90,964 KB
testcase_30 AC 1,422 ms
90,968 KB
testcase_31 AC 1,416 ms
90,828 KB
testcase_32 AC 1,447 ms
90,828 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

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