結果

問題 No.2563 色ごとのグループ
ユーザー mymelochanmymelochan
提出日時 2023-12-02 14:48:26
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 516 ms / 2,000 ms
コード長 1,074 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 107,456 KB
最終ジャッジ日時 2023-12-02 14:48:38
合計ジャッジ時間 7,346 ms
ジャッジサーバーID
(参考情報)
judge11 / judge9
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,724 KB
testcase_01 AC 44 ms
55,724 KB
testcase_02 AC 45 ms
55,724 KB
testcase_03 AC 45 ms
55,724 KB
testcase_04 AC 45 ms
55,724 KB
testcase_05 AC 45 ms
55,720 KB
testcase_06 AC 44 ms
55,724 KB
testcase_07 AC 43 ms
55,724 KB
testcase_08 AC 44 ms
55,720 KB
testcase_09 AC 45 ms
55,724 KB
testcase_10 AC 44 ms
55,724 KB
testcase_11 AC 45 ms
55,724 KB
testcase_12 AC 45 ms
55,720 KB
testcase_13 AC 45 ms
55,720 KB
testcase_14 AC 68 ms
68,740 KB
testcase_15 AC 61 ms
68,712 KB
testcase_16 AC 61 ms
68,712 KB
testcase_17 AC 61 ms
68,712 KB
testcase_18 AC 56 ms
64,432 KB
testcase_19 AC 66 ms
74,088 KB
testcase_20 AC 77 ms
77,312 KB
testcase_21 AC 74 ms
76,520 KB
testcase_22 AC 75 ms
76,520 KB
testcase_23 AC 75 ms
76,648 KB
testcase_24 AC 163 ms
89,088 KB
testcase_25 AC 133 ms
91,544 KB
testcase_26 AC 162 ms
101,060 KB
testcase_27 AC 148 ms
107,456 KB
testcase_28 AC 179 ms
103,384 KB
testcase_29 AC 238 ms
107,068 KB
testcase_30 AC 209 ms
107,068 KB
testcase_31 AC 209 ms
107,068 KB
testcase_32 AC 239 ms
107,068 KB
testcase_33 AC 484 ms
106,772 KB
testcase_34 AC 502 ms
107,276 KB
testcase_35 AC 498 ms
107,276 KB
testcase_36 AC 516 ms
106,772 KB
testcase_37 AC 486 ms
106,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#############################################################

import sys
sys.setrecursionlimit(10**7)

from heapq import heappop,heappush
from collections import deque,defaultdict,Counter
from bisect import bisect_left, bisect_right
from itertools import product,combinations,permutations
from math import sin,cos
#from math import isqrt #DO NOT USE PyPy

ipt = sys.stdin.readline
iin = lambda :int(ipt())
lmin = lambda :list(map(int,ipt().split()))

#############################################################

N,M = lmin()
C = lmin()
G = [[] for _ in range(N)]
for _ in range(M):
    u,v = lmin()
    u,v = u-1,v-1
    if C[u] == C[v]:
        G[u].append(v)
        G[v].append(u)

cnt = [0]*(N+1)
visited = [0]*N
for i in range(N):
    if visited[i]:
        continue

    cnt[C[i]] += 1
    visited[i] = 1
    st = [i]
    while st:
        cur = st.pop()
        for nxt in G[cur]:
            if visited[nxt]:
                continue
            visited[nxt] = 1
            st.append(nxt)
    
ans = 0
for i in range(N+1):
    ans += max(0, cnt[i]-1)

print(ans)
0