結果

問題 No.2563 色ごとのグループ
ユーザー RYOH2718RYOH2718
提出日時 2023-12-02 15:53:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,708 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 113,404 KB
最終ジャッジ日時 2023-12-02 15:53:46
合計ジャッジ時間 8,300 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 35 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 AC 36 ms
53,460 KB
testcase_09 AC 38 ms
53,460 KB
testcase_10 AC 36 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
testcase_12 AC 38 ms
53,460 KB
testcase_13 AC 37 ms
53,460 KB
testcase_14 AC 73 ms
72,992 KB
testcase_15 AC 75 ms
72,988 KB
testcase_16 AC 77 ms
73,756 KB
testcase_17 AC 74 ms
72,988 KB
testcase_18 AC 54 ms
66,560 KB
testcase_19 AC 80 ms
76,164 KB
testcase_20 AC 95 ms
76,916 KB
testcase_21 AC 101 ms
76,548 KB
testcase_22 AC 94 ms
76,420 KB
testcase_23 AC 112 ms
77,060 KB
testcase_24 AC 209 ms
88,428 KB
testcase_25 AC 196 ms
90,252 KB
testcase_26 AC 247 ms
99,236 KB
testcase_27 AC 252 ms
109,612 KB
testcase_28 AC 266 ms
102,224 KB
testcase_29 AC 306 ms
110,072 KB
testcase_30 AC 328 ms
110,072 KB
testcase_31 AC 322 ms
110,072 KB
testcase_32 AC 338 ms
110,072 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

def INT():
    return int(input())


def MI():
    return map(int, input().split())


def LI():
    return list(map(int, input().split()))


class UnionFind:
    def __init__(self, N):
        self.root = [_ for _ in range(N)]
        self.rank = [0] * N
        self.size = [1] * N

    def find(self, x):
        if self.root[x] == x:
            return x
        else:
            # 根を予め取得し,経路圧縮する
            self.root[x] = self.find(self.root[x])
            return self.root[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        else:
            if self.rank[x] > self.rank[y]:
                self.size[x] += self.size[y]
                self.root[y] = x
            else:
                self.size[y] += self.size[x]
                self.root[x] = y
                if self.rank[x] == self.rank[y]:
                    self.rank[y] += 1

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

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


N, M = MI()
C = LI()
dsu = UnionFind(N)
for _ in range(M):
    u, v = MI()
    u -= 1
    v -= 1
    if C[u] == C[v]:
        dsu.union(u, v)

cl = [[] for _ in range(N)]
for i in range(N):
    cl[C[i] - 1].append(i)

ans = 0
for i in range(N):
    if len(cl[i]) < 1:
        continue
    root_c = set()
    all_one = True
    for c in cl[i]:
        root_c.add(dsu.root[c])
        if dsu.getSizeOfSet(c) != 1:
            all_one = False
    if all_one:
        ans += len(cl[i]) - 1
    else:
        ans += len(root_c) - 1

print(ans)
0