結果

問題 No.2563 色ごとのグループ
ユーザー MMMM
提出日時 2023-12-02 17:34:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,335 ms / 2,000 ms
コード長 941 bytes
コンパイル時間 692 ms
コンパイル使用メモリ 11,904 KB
実行使用メモリ 34,624 KB
最終ジャッジ日時 2023-12-02 17:34:32
合計ジャッジ時間 17,107 ms
ジャッジサーバーID
(参考情報)
judge9 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
9,984 KB
testcase_01 AC 30 ms
9,984 KB
testcase_02 AC 31 ms
9,984 KB
testcase_03 AC 32 ms
9,984 KB
testcase_04 AC 31 ms
9,984 KB
testcase_05 AC 31 ms
9,984 KB
testcase_06 AC 31 ms
9,984 KB
testcase_07 AC 30 ms
9,984 KB
testcase_08 AC 31 ms
9,984 KB
testcase_09 AC 32 ms
9,984 KB
testcase_10 AC 30 ms
9,984 KB
testcase_11 AC 32 ms
9,984 KB
testcase_12 AC 30 ms
9,984 KB
testcase_13 AC 31 ms
9,984 KB
testcase_14 AC 35 ms
10,240 KB
testcase_15 AC 38 ms
10,240 KB
testcase_16 AC 38 ms
10,240 KB
testcase_17 AC 38 ms
10,112 KB
testcase_18 AC 33 ms
10,240 KB
testcase_19 AC 55 ms
10,240 KB
testcase_20 AC 85 ms
12,800 KB
testcase_21 AC 65 ms
11,136 KB
testcase_22 AC 62 ms
10,752 KB
testcase_23 AC 90 ms
11,008 KB
testcase_24 AC 604 ms
20,588 KB
testcase_25 AC 497 ms
21,744 KB
testcase_26 AC 625 ms
26,372 KB
testcase_27 AC 467 ms
33,736 KB
testcase_28 AC 729 ms
31,004 KB
testcase_29 AC 1,012 ms
34,624 KB
testcase_30 AC 1,013 ms
34,624 KB
testcase_31 AC 975 ms
34,624 KB
testcase_32 AC 988 ms
34,624 KB
testcase_33 AC 1,321 ms
33,328 KB
testcase_34 AC 1,299 ms
34,288 KB
testcase_35 AC 1,318 ms
34,288 KB
testcase_36 AC 1,315 ms
33,328 KB
testcase_37 AC 1,335 ms
33,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, size):
        self.parent = [i for i in range(size)]

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def merge(self, x, y):
        x_root = self.find(x)
        y_root = self.find(y)
        if x_root != y_root:
            self.parent[x_root] = y_root

    def leader(self, x):
        return self.find(x)


# Read N and M
N, M = map(int, input().split())

# Read C
C = list(map(int, input().split()))

# Initialize UnionFind
uf = UnionFind(N)

# Process the edges
for _ in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if C[u] == C[v]:
        uf.merge(u, v)

# Count the groups
mp = {}
for i in range(N):
    leader = uf.leader(i)
    if leader == i:
        mp[C[i]] = mp.get(C[i], 0) + 1

# Calculate the answer
ans = sum(value - 1 for value in mp.values())
print(ans)
0