結果

問題 No.2563 色ごとのグループ
ユーザー 👑 timitimi
提出日時 2023-12-02 14:55:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 772 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 135,376 KB
最終ジャッジ日時 2023-12-02 14:56:05
合計ジャッジ時間 6,740 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 39 ms
53,460 KB
testcase_03 AC 34 ms
53,460 KB
testcase_04 AC 31 ms
53,460 KB
testcase_05 AC 33 ms
53,460 KB
testcase_06 AC 31 ms
53,460 KB
testcase_07 AC 32 ms
53,460 KB
testcase_08 AC 33 ms
53,460 KB
testcase_09 AC 34 ms
53,460 KB
testcase_10 AC 33 ms
53,460 KB
testcase_11 AC 34 ms
53,460 KB
testcase_12 AC 31 ms
53,460 KB
testcase_13 AC 32 ms
53,460 KB
testcase_14 AC 59 ms
71,040 KB
testcase_15 AC 59 ms
71,040 KB
testcase_16 AC 58 ms
71,040 KB
testcase_17 AC 58 ms
71,040 KB
testcase_18 AC 42 ms
62,348 KB
testcase_19 AC 80 ms
74,240 KB
testcase_20 AC 75 ms
78,876 KB
testcase_21 AC 73 ms
76,672 KB
testcase_22 AC 69 ms
76,416 KB
testcase_23 AC 74 ms
76,544 KB
testcase_24 AC 173 ms
98,392 KB
testcase_25 AC 156 ms
102,048 KB
testcase_26 AC 194 ms
116,372 KB
testcase_27 AC 197 ms
134,860 KB
testcase_28 AC 247 ms
121,100 KB
testcase_29 AC 266 ms
135,376 KB
testcase_30 AC 265 ms
135,340 KB
testcase_31 AC 269 ms
135,356 KB
testcase_32 AC 262 ms
135,344 KB
testcase_33 AC 402 ms
113,592 KB
testcase_34 AC 384 ms
113,004 KB
testcase_35 AC 386 ms
113,004 KB
testcase_36 AC 432 ms
113,592 KB
testcase_37 AC 376 ms
113,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int, input().split())
C=list(map(int, input().split()))
par=[i for i in range(N)]
rank=[0]*(N)
friend=[0]*N
block=[0]*N
size=[1]*N
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 union(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return 
  if rank[x]>rank[y]:
    par[y]=x
    size[x]+=size[y]
  else:
    par[x]=y
    size[y]+=size[x]
    if rank[x]==rank[y]:
      rank[y]+=1
 
 
for i in range(M):
  a,b=map(int,input().split())
  a-=1;b-=1
  if C[a]==C[b]:
    union(a,b)
    
ans=0
D={};E={}
for i in range(N):
  d=find(i)
  if d not in D:
    D[d]=1
    c=C[i]
    if c not in E:
      E[c]=0
    E[c]+=1

for e in E:
  ans+=E[e]-1
print(ans)
0