結果

問題 No.2563 色ごとのグループ
ユーザー kemunikukemuniku
提出日時 2023-12-02 15:02:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 1,157 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 121,872 KB
最終ジャッジ日時 2023-12-02 15:02:43
合計ジャッジ時間 7,329 ms
ジャッジサーバーID
(参考情報)
judge13 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
55,612 KB
testcase_01 AC 38 ms
55,612 KB
testcase_02 AC 37 ms
55,612 KB
testcase_03 AC 36 ms
55,612 KB
testcase_04 AC 35 ms
55,612 KB
testcase_05 AC 36 ms
55,612 KB
testcase_06 AC 35 ms
55,612 KB
testcase_07 AC 36 ms
55,612 KB
testcase_08 AC 36 ms
55,612 KB
testcase_09 AC 49 ms
55,612 KB
testcase_10 AC 36 ms
55,612 KB
testcase_11 AC 38 ms
55,612 KB
testcase_12 AC 36 ms
55,612 KB
testcase_13 AC 37 ms
55,612 KB
testcase_14 AC 64 ms
70,992 KB
testcase_15 AC 71 ms
73,120 KB
testcase_16 AC 65 ms
73,116 KB
testcase_17 AC 64 ms
73,116 KB
testcase_18 AC 48 ms
64,432 KB
testcase_19 AC 71 ms
76,448 KB
testcase_20 AC 81 ms
78,584 KB
testcase_21 AC 77 ms
77,000 KB
testcase_22 AC 77 ms
76,776 KB
testcase_23 AC 101 ms
76,884 KB
testcase_24 AC 185 ms
103,096 KB
testcase_25 AC 184 ms
107,036 KB
testcase_26 AC 228 ms
110,580 KB
testcase_27 AC 251 ms
121,416 KB
testcase_28 AC 252 ms
111,784 KB
testcase_29 AC 298 ms
121,768 KB
testcase_30 AC 281 ms
121,868 KB
testcase_31 AC 286 ms
121,868 KB
testcase_32 AC 272 ms
121,872 KB
testcase_33 AC 395 ms
106,612 KB
testcase_34 AC 416 ms
106,044 KB
testcase_35 AC 413 ms
106,044 KB
testcase_36 AC 389 ms
106,612 KB
testcase_37 AC 381 ms
106,612 KB
権限があれば一括ダウンロードができます

ソースコード

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(int)
for i in range(N):
    d[C[i]] += 1
cost = {}
for c in d:
    cost[c] = d[c]-1
uf = UnionFind(N)
for i in range(M):
    u,v = map(int,input().split())
    u-=1
    v-=1
    if C[u] == C[v]:
        if not uf.issame(u,v):
            uf.unite(u,v)
            cost[C[u]] -= 1
ans = 0
for c in d:
    x = cost[c]
    ans += x
print(ans)
0