結果

問題 No.2563 色ごとのグループ
ユーザー tetztetz
提出日時 2023-12-02 16:16:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 933 bytes
コンパイル時間 327 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 113,332 KB
最終ジャッジ日時 2023-12-02 16:16:25
合計ジャッジ時間 7,540 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,616 KB
testcase_01 AC 41 ms
55,616 KB
testcase_02 AC 46 ms
55,616 KB
testcase_03 AC 40 ms
55,616 KB
testcase_04 AC 40 ms
55,616 KB
testcase_05 AC 40 ms
55,616 KB
testcase_06 AC 41 ms
55,616 KB
testcase_07 AC 39 ms
55,616 KB
testcase_08 AC 39 ms
55,616 KB
testcase_09 AC 43 ms
55,616 KB
testcase_10 AC 40 ms
55,616 KB
testcase_11 AC 42 ms
55,616 KB
testcase_12 AC 41 ms
55,616 KB
testcase_13 AC 41 ms
55,616 KB
testcase_14 AC 70 ms
73,096 KB
testcase_15 AC 70 ms
73,084 KB
testcase_16 AC 70 ms
73,116 KB
testcase_17 AC 70 ms
73,116 KB
testcase_18 AC 49 ms
64,444 KB
testcase_19 AC 78 ms
76,412 KB
testcase_20 AC 94 ms
77,740 KB
testcase_21 AC 81 ms
76,748 KB
testcase_22 AC 80 ms
76,648 KB
testcase_23 AC 86 ms
76,752 KB
testcase_24 AC 174 ms
94,468 KB
testcase_25 AC 159 ms
97,320 KB
testcase_26 AC 195 ms
111,488 KB
testcase_27 AC 184 ms
106,492 KB
testcase_28 AC 239 ms
113,332 KB
testcase_29 AC 257 ms
106,440 KB
testcase_30 AC 259 ms
106,568 KB
testcase_31 AC 256 ms
106,568 KB
testcase_32 AC 258 ms
106,568 KB
testcase_33 AC 409 ms
106,488 KB
testcase_34 AC 380 ms
105,920 KB
testcase_35 AC 382 ms
105,920 KB
testcase_36 AC 432 ms
106,488 KB
testcase_37 AC 406 ms
106,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict as dd


class UnionFind:
    def __init__(self, n):
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[y]
        self.parents[y] = x


N, M = map(int, input().split())
c = list(map(int, input().split()))
gc = dd(lambda: -1)
for ci in c:
    gc[ci] += 1
uf = UnionFind(N)
for _ in range(M):
    u, v = map(lambda x: int(x) - 1, input().split())
    if c[u] == c[v] and uf.find(u) != uf.find(v):
        uf.union(u, v)
        gc[c[u]] -= 1
# print(sum(gc.values()) - len(gc.keys()))
print(sum(gc.values()))
0