結果

問題 No.2563 色ごとのグループ
ユーザー ありあけありあけ
提出日時 2024-09-28 14:45:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,009 ms / 2,000 ms
コード長 1,483 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 35,644 KB
最終ジャッジ日時 2024-09-28 14:45:48
合計ジャッジ時間 12,948 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
10,752 KB
testcase_01 AC 26 ms
10,880 KB
testcase_02 AC 25 ms
10,880 KB
testcase_03 AC 28 ms
10,880 KB
testcase_04 AC 29 ms
10,752 KB
testcase_05 AC 30 ms
10,752 KB
testcase_06 AC 29 ms
10,752 KB
testcase_07 AC 25 ms
10,880 KB
testcase_08 AC 25 ms
11,008 KB
testcase_09 AC 25 ms
10,752 KB
testcase_10 AC 25 ms
10,880 KB
testcase_11 AC 25 ms
10,752 KB
testcase_12 AC 26 ms
11,008 KB
testcase_13 AC 24 ms
11,008 KB
testcase_14 AC 29 ms
11,008 KB
testcase_15 AC 32 ms
10,880 KB
testcase_16 AC 38 ms
11,008 KB
testcase_17 AC 33 ms
11,008 KB
testcase_18 AC 28 ms
11,008 KB
testcase_19 AC 48 ms
11,008 KB
testcase_20 AC 75 ms
13,340 KB
testcase_21 AC 54 ms
12,348 KB
testcase_22 AC 56 ms
11,392 KB
testcase_23 AC 83 ms
11,648 KB
testcase_24 AC 500 ms
21,460 KB
testcase_25 AC 389 ms
21,848 KB
testcase_26 AC 487 ms
29,524 KB
testcase_27 AC 326 ms
34,200 KB
testcase_28 AC 615 ms
31,276 KB
testcase_29 AC 781 ms
35,536 KB
testcase_30 AC 780 ms
35,644 KB
testcase_31 AC 795 ms
35,528 KB
testcase_32 AC 753 ms
35,516 KB
testcase_33 AC 984 ms
31,056 KB
testcase_34 AC 984 ms
30,740 KB
testcase_35 AC 1,009 ms
30,864 KB
testcase_36 AC 939 ms
31,064 KB
testcase_37 AC 976 ms
30,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
N,M = map(int,input().split())
C = list(map(int,input().split()))
if len(set(C)) == len(C):
    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
    if par[x] < par[y]:
        x,y = y,x
    par[x] = y

def members(x):
    root = find(x)
    return [i for i in range(N) if find(i) == root]

def roots():
    return [i for i,x in enumerate(par) if x == i]

def group_count():
    k = roots()
    return len(k)

def all_group_menbers():
    group_members = defaultdict(list)
    for member in range(N):
        group_members[find(member)].append(member)
    return group_members

for m in range(M):
    u,v = map(int,input().split())
    if C[u-1] == C[v-1]:
        unite(u,v)

print(group_count()-1 - len(set(C)))
#print(graph)
#print(C)
#print(set(C))
#print(len(set(C)))
#print(par)
#print(set(par))
#print(len(set(par))-1)
0