結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,240 KB
testcase_01 AC 25 ms
10,240 KB
testcase_02 AC 25 ms
10,240 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 26 ms
10,240 KB
testcase_06 AC 27 ms
10,240 KB
testcase_07 AC 26 ms
10,240 KB
testcase_08 RE -
testcase_09 AC 29 ms
10,240 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 33 ms
10,624 KB
testcase_16 AC 38 ms
10,624 KB
testcase_17 WA -
testcase_18 AC 30 ms
10,624 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 554 ms
35,312 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 1,231 ms
64,228 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 1,207 ms
33,744 KB
testcase_34 RE -
testcase_35 AC 1,302 ms
33,548 KB
testcase_36 RE -
testcase_37 AC 1,288 ms
33,744 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 = dd(list)
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)
    col_root[C[u]] = [uf.root(u),u]
ans = 0
#print(col_root)
for key in col.keys():
    n = len(col[key])
    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