結果

問題 No.2563 色ごとのグループ
ユーザー CecilCecil
提出日時 2023-12-02 16:06:20
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 1,865 bytes
コンパイル時間 622 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 71,056 KB
最終ジャッジ日時 2023-12-02 16:06:37
合計ジャッジ時間 14,709 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
10,240 KB
testcase_01 AC 27 ms
10,240 KB
testcase_02 AC 26 ms
10,240 KB
testcase_03 AC 25 ms
10,240 KB
testcase_04 AC 26 ms
10,240 KB
testcase_05 AC 27 ms
10,240 KB
testcase_06 AC 27 ms
10,240 KB
testcase_07 AC 26 ms
10,240 KB
testcase_08 AC 25 ms
10,240 KB
testcase_09 AC 26 ms
10,240 KB
testcase_10 AC 25 ms
10,240 KB
testcase_11 AC 27 ms
10,240 KB
testcase_12 AC 26 ms
10,240 KB
testcase_13 AC 26 ms
10,240 KB
testcase_14 AC 32 ms
10,624 KB
testcase_15 AC 34 ms
10,624 KB
testcase_16 AC 32 ms
10,496 KB
testcase_17 AC 33 ms
10,496 KB
testcase_18 AC 30 ms
10,624 KB
testcase_19 AC 45 ms
10,624 KB
testcase_20 AC 90 ms
14,592 KB
testcase_21 AC 77 ms
12,288 KB
testcase_22 AC 58 ms
11,520 KB
testcase_23 AC 83 ms
12,032 KB
testcase_24 AC 599 ms
31,192 KB
testcase_25 AC 549 ms
34,180 KB
testcase_26 AC 753 ms
44,320 KB
testcase_27 AC 821 ms
60,284 KB
testcase_28 AC 924 ms
51,744 KB
testcase_29 AC 1,202 ms
61,400 KB
testcase_30 AC 1,185 ms
61,392 KB
testcase_31 AC 1,195 ms
61,408 KB
testcase_32 AC 1,219 ms
61,416 KB
testcase_33 TLE -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

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)
G = [ [] for _ in range(N+1) ]
col = 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)
    G[u].append(v)
    G[v].append(u)
ans = 0
for key in col.keys():
    for i in col[key]:
        for j in col[key]:
            if not uf.issame(i,j):
                uf.unite(i,j)
                ans += 1
print(ans)
0