結果

問題 No.2563 色ごとのグループ
ユーザー titiatitia
提出日時 2023-12-12 03:05:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 502 ms / 2,000 ms
コード長 878 bytes
コンパイル時間 358 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 120,340 KB
最終ジャッジ日時 2023-12-12 03:05:24
合計ジャッジ時間 11,652 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
104,324 KB
testcase_01 AC 94 ms
104,324 KB
testcase_02 AC 96 ms
104,324 KB
testcase_03 AC 93 ms
104,324 KB
testcase_04 AC 94 ms
104,324 KB
testcase_05 AC 95 ms
104,324 KB
testcase_06 AC 94 ms
104,324 KB
testcase_07 AC 93 ms
104,324 KB
testcase_08 AC 118 ms
104,196 KB
testcase_09 AC 97 ms
103,300 KB
testcase_10 AC 96 ms
104,196 KB
testcase_11 AC 95 ms
103,300 KB
testcase_12 AC 96 ms
103,812 KB
testcase_13 AC 95 ms
103,812 KB
testcase_14 AC 107 ms
92,052 KB
testcase_15 AC 105 ms
91,412 KB
testcase_16 AC 111 ms
91,412 KB
testcase_17 AC 111 ms
91,540 KB
testcase_18 AC 115 ms
102,932 KB
testcase_19 AC 136 ms
114,664 KB
testcase_20 AC 143 ms
111,024 KB
testcase_21 AC 140 ms
114,688 KB
testcase_22 AC 144 ms
115,248 KB
testcase_23 AC 141 ms
107,056 KB
testcase_24 AC 231 ms
112,076 KB
testcase_25 AC 223 ms
117,796 KB
testcase_26 AC 261 ms
119,736 KB
testcase_27 AC 242 ms
119,772 KB
testcase_28 AC 262 ms
104,248 KB
testcase_29 AC 361 ms
120,012 KB
testcase_30 AC 320 ms
119,888 KB
testcase_31 AC 324 ms
119,888 KB
testcase_32 AC 325 ms
119,888 KB
testcase_33 AC 480 ms
120,220 KB
testcase_34 AC 470 ms
120,340 KB
testcase_35 AC 502 ms
120,340 KB
testcase_36 AC 468 ms
119,968 KB
testcase_37 AC 471 ms
120,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

# UnionFind

Group = [i for i in range(N)] # グループ分け
Nodes = [1]*(N) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(M):
    x,y=map(int,input().split())
    if C[x-1]==C[y-1]:
        Union(x-1,y-1)


LIST=[[] for i in range(10**6)]

for i in range(N):
    if find(i)==i:
        LIST[C[i]].append(i)
ANS=0
for i in range(10**6):
    ANS+=max(0,len(LIST[i])-1)

print(ANS)

0