結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
62,408 KB
testcase_01 AC 42 ms
55,604 KB
testcase_02 AC 41 ms
55,604 KB
testcase_03 AC 45 ms
55,604 KB
testcase_04 AC 42 ms
55,604 KB
testcase_05 AC 42 ms
55,604 KB
testcase_06 AC 42 ms
55,604 KB
testcase_07 AC 41 ms
55,604 KB
testcase_08 AC 42 ms
55,604 KB
testcase_09 AC 47 ms
61,584 KB
testcase_10 AC 42 ms
55,604 KB
testcase_11 AC 47 ms
61,584 KB
testcase_12 AC 53 ms
64,692 KB
testcase_13 AC 47 ms
61,584 KB
testcase_14 AC 74 ms
73,060 KB
testcase_15 AC 77 ms
70,968 KB
testcase_16 AC 77 ms
71,488 KB
testcase_17 AC 77 ms
70,944 KB
testcase_18 AC 63 ms
68,660 KB
testcase_19 AC 221 ms
76,924 KB
testcase_20 AC 369 ms
77,836 KB
testcase_21 AC 271 ms
77,544 KB
testcase_22 AC 283 ms
77,564 KB
testcase_23 AC 839 ms
79,276 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
Clist = [[] for _ in range(N + 1)]
for i in range(N):
    Clist[C[i]].append(i)
isvisited = [False for _ in range(N)]
dq = deque()
for i in range(1, N + 1):
    comp = 0
    for v in Clist[i]:
        if isvisited[v]:
            continue
        comp += 1
        isvisited[v] = True
        dq.append(v)
        while len(dq):
            p = dq.popleft()
            for x in graph[p]:
                if isvisited[x]:
                    continue
                isvisited[x] = True
                dq.append(x)
    ans += max(0, comp - 1)
print(ans)
0