結果

問題 No.2563 色ごとのグループ
ユーザー kusirakusirakusirakusira
提出日時 2023-09-19 14:56:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 489 ms / 2,000 ms
コード長 1,904 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 152,616 KB
最終ジャッジ日時 2023-12-02 14:00:27
合計ジャッジ時間 8,445 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
55,608 KB
testcase_01 AC 36 ms
55,608 KB
testcase_02 AC 37 ms
55,608 KB
testcase_03 AC 36 ms
55,608 KB
testcase_04 AC 38 ms
55,608 KB
testcase_05 AC 36 ms
55,608 KB
testcase_06 AC 36 ms
55,608 KB
testcase_07 AC 37 ms
55,608 KB
testcase_08 AC 37 ms
55,608 KB
testcase_09 AC 39 ms
55,608 KB
testcase_10 AC 36 ms
55,608 KB
testcase_11 AC 39 ms
55,608 KB
testcase_12 AC 38 ms
55,608 KB
testcase_13 AC 38 ms
55,608 KB
testcase_14 AC 74 ms
73,336 KB
testcase_15 AC 70 ms
73,976 KB
testcase_16 AC 71 ms
74,104 KB
testcase_17 AC 69 ms
73,720 KB
testcase_18 AC 48 ms
61,920 KB
testcase_19 AC 85 ms
77,816 KB
testcase_20 AC 91 ms
80,068 KB
testcase_21 AC 80 ms
78,164 KB
testcase_22 AC 81 ms
78,436 KB
testcase_23 AC 97 ms
80,728 KB
testcase_24 AC 246 ms
113,768 KB
testcase_25 AC 202 ms
107,812 KB
testcase_26 AC 254 ms
118,960 KB
testcase_27 AC 189 ms
120,976 KB
testcase_28 AC 276 ms
132,996 KB
testcase_29 AC 353 ms
152,612 KB
testcase_30 AC 340 ms
152,616 KB
testcase_31 AC 357 ms
152,612 KB
testcase_32 AC 333 ms
152,616 KB
testcase_33 AC 457 ms
134,940 KB
testcase_34 AC 489 ms
135,016 KB
testcase_35 AC 471 ms
134,812 KB
testcase_36 AC 469 ms
134,000 KB
testcase_37 AC 472 ms
133,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind
from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = 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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
        
        

n, m = map(int,input().split())
C = list(map(int,input().split()))
uf = UnionFind(n)

if((1<=n<=2*10**5)==False): print("WA")
if((0<=m<=min(2*10**5,n*(n-1)//2))==False): print("WA")
if((1<=min(C) and max(C)<=n)==False): print("WA")

M = set()
for _ in range(m):
    a, b = map(int,input().split())
    if((1<=a<=n)==False): print("WA")
    if((1<=b<=n)==False): print("WA")
    if(a==b): print("WA")
    if((a,b) in M): print("WA")
    M.add((a,b))
    M.add((b,a))
    a -= 1
    b -= 1
    if(C[a] == C[b]):
        uf.union(a, b)

print(uf.group_count() - len(set(C)))









0