結果

問題 No.2563 色ごとのグループ
ユーザー AngrySadEightAngrySadEight
提出日時 2023-11-24 00:58:27
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 857 bytes
コンパイル時間 168 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 96,100 KB
最終ジャッジ日時 2023-12-02 14:01:33
合計ジャッジ時間 7,429 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
55,604 KB
testcase_01 AC 41 ms
55,604 KB
testcase_02 AC 41 ms
55,604 KB
testcase_03 AC 40 ms
55,604 KB
testcase_04 AC 40 ms
55,604 KB
testcase_05 AC 40 ms
55,604 KB
testcase_06 AC 41 ms
55,604 KB
testcase_07 AC 40 ms
55,604 KB
testcase_08 AC 42 ms
55,604 KB
testcase_09 AC 49 ms
61,588 KB
testcase_10 AC 42 ms
55,604 KB
testcase_11 AC 46 ms
61,588 KB
testcase_12 AC 52 ms
64,692 KB
testcase_13 AC 46 ms
61,588 KB
testcase_14 AC 75 ms
73,580 KB
testcase_15 AC 73 ms
70,972 KB
testcase_16 AC 72 ms
70,976 KB
testcase_17 AC 75 ms
70,956 KB
testcase_18 AC 59 ms
66,548 KB
testcase_19 AC 208 ms
76,924 KB
testcase_20 AC 355 ms
77,708 KB
testcase_21 AC 251 ms
76,904 KB
testcase_22 AC 269 ms
76,924 KB
testcase_23 AC 815 ms
78,508 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

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
    for i in range(M):
        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