結果

問題 No.2563 色ごとのグループ
ユーザー u_kunu_kun
提出日時 2023-12-02 16:13:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 640 ms / 2,000 ms
コード長 861 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 130,728 KB
最終ジャッジ日時 2023-12-02 16:13:20
合計ジャッジ時間 11,459 ms
ジャッジサーバーID
(参考情報)
judge12 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,604 KB
testcase_01 AC 41 ms
55,604 KB
testcase_02 AC 41 ms
55,604 KB
testcase_03 AC 41 ms
55,604 KB
testcase_04 AC 41 ms
55,604 KB
testcase_05 AC 43 ms
55,604 KB
testcase_06 AC 42 ms
55,604 KB
testcase_07 AC 41 ms
55,604 KB
testcase_08 AC 41 ms
55,604 KB
testcase_09 AC 44 ms
55,604 KB
testcase_10 AC 46 ms
55,604 KB
testcase_11 AC 44 ms
55,604 KB
testcase_12 AC 42 ms
55,604 KB
testcase_13 AC 42 ms
55,604 KB
testcase_14 AC 115 ms
76,576 KB
testcase_15 AC 86 ms
76,580 KB
testcase_16 AC 86 ms
76,576 KB
testcase_17 AC 84 ms
76,576 KB
testcase_18 AC 55 ms
66,508 KB
testcase_19 AC 87 ms
76,576 KB
testcase_20 AC 114 ms
79,700 KB
testcase_21 AC 105 ms
77,544 KB
testcase_22 AC 95 ms
76,956 KB
testcase_23 AC 113 ms
77,180 KB
testcase_24 AC 332 ms
97,484 KB
testcase_25 AC 293 ms
96,096 KB
testcase_26 AC 376 ms
104,920 KB
testcase_27 AC 333 ms
110,576 KB
testcase_28 AC 433 ms
109,420 KB
testcase_29 AC 552 ms
119,888 KB
testcase_30 AC 581 ms
118,556 KB
testcase_31 AC 562 ms
120,056 KB
testcase_32 AC 581 ms
120,052 KB
testcase_33 AC 618 ms
125,480 KB
testcase_34 AC 640 ms
130,728 KB
testcase_35 AC 612 ms
128,092 KB
testcase_36 AC 637 ms
127,788 KB
testcase_37 AC 608 ms
127,812 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N, M = map(int, input().split())
C = list(map(int, input().split()))

#色iの連結成分の個数はcnt[i]個
cnt = [0 for _ in range(N + 1)]

G = [[] for _ in range(N + 1)]

for _ in range(M):
    a, b = map(int, input().split())
    G[a].append(b)
    G[b].append(a)

vis = set()

for s_id in range(1, N + 1):
    if s_id in vis:
        continue
    
    vis.add(s_id)
    FIFO = deque()
    FIFO.append(s_id)
    
    color = C[s_id - 1]
    
    cnt[color] += 1
    
    while FIFO:
        now_vid = FIFO.popleft()
        
        for ne_vid in G[now_vid]:
            if ne_vid not in vis and C[now_vid - 1] == C[ne_vid - 1]:
                vis.add(ne_vid)
                FIFO.append(ne_vid)
                
        
ans = 0
for i in range(len(cnt)):
    if cnt[i] >= 2:
        ans += cnt[i] - 1

print(ans)








0