結果

問題 No.2563 色ごとのグループ
ユーザー AngrySadEightAngrySadEight
提出日時 2023-11-24 01:36:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 862 bytes
コンパイル時間 209 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 108,228 KB
最終ジャッジ日時 2023-12-02 14:01:41
合計ジャッジ時間 5,725 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
55,604 KB
testcase_01 AC 37 ms
55,604 KB
testcase_02 AC 35 ms
55,604 KB
testcase_03 AC 37 ms
55,604 KB
testcase_04 AC 36 ms
55,604 KB
testcase_05 AC 37 ms
55,604 KB
testcase_06 AC 35 ms
55,604 KB
testcase_07 AC 50 ms
55,604 KB
testcase_08 AC 35 ms
55,604 KB
testcase_09 AC 36 ms
55,604 KB
testcase_10 AC 34 ms
55,604 KB
testcase_11 AC 35 ms
55,604 KB
testcase_12 AC 36 ms
55,604 KB
testcase_13 AC 36 ms
55,604 KB
testcase_14 AC 51 ms
66,776 KB
testcase_15 AC 51 ms
68,888 KB
testcase_16 AC 50 ms
68,892 KB
testcase_17 AC 51 ms
66,768 KB
testcase_18 AC 46 ms
64,440 KB
testcase_19 AC 51 ms
70,928 KB
testcase_20 AC 85 ms
77,396 KB
testcase_21 AC 56 ms
72,972 KB
testcase_22 AC 54 ms
72,988 KB
testcase_23 AC 62 ms
76,860 KB
testcase_24 AC 109 ms
88,576 KB
testcase_25 AC 100 ms
90,644 KB
testcase_26 AC 125 ms
100,044 KB
testcase_27 AC 132 ms
108,228 KB
testcase_28 AC 173 ms
103,452 KB
testcase_29 AC 160 ms
107,724 KB
testcase_30 AC 156 ms
107,724 KB
testcase_31 AC 157 ms
107,724 KB
testcase_32 AC 160 ms
107,724 KB
testcase_33 AC 361 ms
107,536 KB
testcase_34 AC 322 ms
107,932 KB
testcase_35 AC 349 ms
107,932 KB
testcase_36 AC 357 ms
107,536 KB
testcase_37 AC 332 ms
107,536 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
C = list(map(int, input().split()))
graph = [[] for _ in range(N)]
for i in range(M):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if C[u] == C[v]:
        graph[u].append(v)
        graph[v].append(u)

ans = 0
Clen = [0 for _ in range(N + 1)]
for i in range(N):
    Clen[C[i]] += 1
isvisited = [False for _ in range(N)]
dq = deque()
for i in range(N):
    if isvisited[i]:
        continue
    isvisited[i] = True
    dq.append(i)
    while len(dq):
        Clen[C[i]] -= 1
        p = dq.popleft()
        for x in graph[p]:
            if isvisited[x]:
                continue
            isvisited[x] = True
            dq.append(x)
    Clen[C[i]] += 1
# print(Clen)
for i in range(1, N + 1):
    ans += max(0, Clen[i] - 1)
print(ans)


0