結果

問題 No.2563 色ごとのグループ
ユーザー kemunikukemuniku
提出日時 2023-12-02 14:59:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,131 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 122,556 KB
最終ジャッジ日時 2023-12-02 15:00:09
合計ジャッジ時間 6,952 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,612 KB
testcase_01 AC 38 ms
55,612 KB
testcase_02 AC 38 ms
55,612 KB
testcase_03 AC 35 ms
55,612 KB
testcase_04 AC 35 ms
55,612 KB
testcase_05 AC 35 ms
55,612 KB
testcase_06 AC 35 ms
55,612 KB
testcase_07 AC 38 ms
55,612 KB
testcase_08 AC 39 ms
55,612 KB
testcase_09 AC 40 ms
55,612 KB
testcase_10 AC 38 ms
55,612 KB
testcase_11 AC 37 ms
55,612 KB
testcase_12 AC 36 ms
55,612 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 62 ms
73,120 KB
testcase_16 AC 63 ms
72,988 KB
testcase_17 AC 62 ms
72,988 KB
testcase_18 AC 46 ms
64,552 KB
testcase_19 WA -
testcase_20 AC 87 ms
79,000 KB
testcase_21 AC 89 ms
77,496 KB
testcase_22 WA -
testcase_23 AC 95 ms
77,248 KB
testcase_24 AC 176 ms
99,300 KB
testcase_25 AC 162 ms
101,076 KB
testcase_26 AC 202 ms
102,128 KB
testcase_27 AC 201 ms
111,284 KB
testcase_28 AC 221 ms
102,944 KB
testcase_29 AC 279 ms
112,388 KB
testcase_30 WA -
testcase_31 AC 267 ms
112,172 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.count = n
        self.par = [-1]*n
        self.siz = [1]*n
    def root(self,x):
        if(self.par[x] == -1):
            return x
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]
    def issame(self,x,y):
        return self.root(x) == self.root(y)
    def unite(self,x,y):
        x = self.root(x)
        y = self.root(y)
        if(x == y):
            return False
        if(self.siz[x]<self.siz[y]):
            x,y = y,x
        self.par[y] = x
        self.siz[x] += self.siz[y]
        self.count -= 1
        return True
    def size(self,x):
        return self.siz[self.root(x)]
from collections import defaultdict
N,M = map(int,input().split())
C = list(map(int,input().split()))
d = defaultdict(list)
for i in range(N):
    d[C[i]].append(i)
uf = UnionFind(N)
for i in range(M):
    u,v = map(int,input().split())
    u-=1
    v-=1
    if C[u] == C[v]:
        uf.unite(u,v)
ans = 0
for c in d:
    x = d[c]
    for i in range(len(x)-1):
        if not uf.issame(x[i],x[i+1]):
            ans += 1
print(ans)
0