結果

問題 No.2563 色ごとのグループ
ユーザー ありあけありあけ
提出日時 2024-09-28 13:44:42
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,157 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 42,004 KB
最終ジャッジ日時 2024-09-28 13:44:59
合計ジャッジ時間 9,503 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,752 KB
testcase_01 AC 27 ms
10,880 KB
testcase_02 AC 28 ms
10,880 KB
testcase_03 AC 31 ms
10,752 KB
testcase_04 AC 30 ms
10,624 KB
testcase_05 AC 28 ms
10,752 KB
testcase_06 AC 29 ms
10,752 KB
testcase_07 AC 29 ms
10,624 KB
testcase_08 WA -
testcase_09 AC 29 ms
10,752 KB
testcase_10 AC 30 ms
10,752 KB
testcase_11 AC 29 ms
10,752 KB
testcase_12 AC 29 ms
10,880 KB
testcase_13 AC 29 ms
10,752 KB
testcase_14 AC 34 ms
11,264 KB
testcase_15 AC 37 ms
11,008 KB
testcase_16 AC 36 ms
11,008 KB
testcase_17 AC 36 ms
11,136 KB
testcase_18 AC 31 ms
11,136 KB
testcase_19 AC 54 ms
11,008 KB
testcase_20 AC 73 ms
13,168 KB
testcase_21 AC 62 ms
12,176 KB
testcase_22 AC 63 ms
11,520 KB
testcase_23 AC 88 ms
12,032 KB
testcase_24 AC 565 ms
19,912 KB
testcase_25 AC 419 ms
25,068 KB
testcase_26 AC 648 ms
28,332 KB
testcase_27 AC 359 ms
41,316 KB
testcase_28 AC 668 ms
29,552 KB
testcase_29 AC 899 ms
41,924 KB
testcase_30 AC 857 ms
42,004 KB
testcase_31 AC 887 ms
41,928 KB
testcase_32 AC 891 ms
41,992 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
N,M = map(int,input().split())
C = list(map(int,input().split()))
if len(set(C)) == len(C) or len(set(C)) == 1:
    print(0)
    exit()


par = [i for i in range(N+1)]
#parはparent親のこと
#par[x]の値がその木の根=親を表す
#print(par)
def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        #ここ経路圧縮 テクニックですよねえ
        return par[x]

def same(x,y):
    return find(x) == find(y)

def same_print(x,y):
    if find(x) == find(y):
        print(x)
    else:
        return False
    #これ微妙か?intが返されるか
    # Falseが返されるかわかんない上に
    # 0だとFalseになるの最悪かも

def unite(x,y):
    x = find(x)
    y = find(y)
    if x == y:
        return 0
    par[x] = y
    
graph = defaultdict(list)
for m in range(M):
    u,v = map(int,input().split())
    if C[u-1] == C[v-1]:
        graph[u].append(v)
        graph[v].append(u)
        unite(u,v)
#print(graph)
#print(C)
#print(set(C))
#print(len(set(C)))
#print(set(par))
#print(len(set(par))-1)
#
print(len(set(par))-1-len(set(C)))
0