結果

問題 No.2563 色ごとのグループ
ユーザー navel_tosnavel_tos
提出日時 2023-12-02 15:46:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,268 bytes
コンパイル時間 166 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 110,992 KB
最終ジャッジ日時 2023-12-02 15:46:41
合計ジャッジ時間 7,316 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,616 KB
testcase_01 AC 37 ms
55,616 KB
testcase_02 AC 39 ms
55,616 KB
testcase_03 WA -
testcase_04 AC 35 ms
55,616 KB
testcase_05 AC 45 ms
55,616 KB
testcase_06 WA -
testcase_07 AC 37 ms
55,616 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 385 ms
109,176 KB
testcase_34 AC 329 ms
108,608 KB
testcase_35 AC 335 ms
108,608 KB
testcase_36 AC 329 ms
109,176 KB
testcase_37 AC 353 ms
109,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#緑以下G

#UnionFind
class UnionFind:
    def __init__(self,N): self._parent=[-1 for i in[0]*N]
    def find(self,v):  #頂点vの親を探し、経路圧縮する
        vertices=[]
        while self._parent[v]>=0: vertices.append(v);v=self._parent[v]
        for i in vertices: self._parent[i]=v
        return v
    def unite(self,x,y):  #頂点xとyを併合し、併合の有無を返す
        x,y = self.find(x),self.find(y)
        if x==y: return 0
        if self._parent[x]>self._parent[y]: x,y=y,x  #負値で管理
        self._parent[x]+=self._parent[y]; self._parent[y]=x; return 1
    def same(self,x,y):return self.find(x)==self.find(y)   #xとyは同一集合か返す
    def size(self,x):  return -self._parent[self.find(x)]  #xの集合のサイズを求める


from collections import defaultdict

#入力受取
N,M = map(int,input().split())
C = list(map(int,input().split()))
UF = UnionFind(N)
for _ in range(M):
    u,v = map(lambda x: int(x)-1,input().split())
    if C[u] == C[v]: UF.unite(u,v)

#色ごとに連結判定
D = defaultdict(list)
for i,c in enumerate(C): D[c].append(i)
ans = 0
for c in D.keys():
    base = D[c][0]
    for i in range(1,len(D[c])):
        if not UF.same(base,i): UF.unite(base,i); ans += 1
print(ans)
0