結果

問題 No.2563 色ごとのグループ
ユーザー hirayuu_ychirayuu_yc
提出日時 2023-12-02 15:10:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 628 ms / 2,000 ms
コード長 1,175 bytes
コンパイル時間 567 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 129,248 KB
最終ジャッジ日時 2023-12-02 15:10:34
合計ジャッジ時間 9,265 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 35 ms
53,460 KB
testcase_05 AC 36 ms
53,460 KB
testcase_06 AC 38 ms
53,460 KB
testcase_07 AC 36 ms
53,460 KB
testcase_08 AC 39 ms
53,460 KB
testcase_09 AC 37 ms
53,460 KB
testcase_10 AC 37 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
testcase_12 AC 36 ms
53,460 KB
testcase_13 AC 35 ms
53,460 KB
testcase_14 AC 65 ms
73,000 KB
testcase_15 AC 66 ms
73,004 KB
testcase_16 AC 65 ms
73,000 KB
testcase_17 AC 64 ms
73,000 KB
testcase_18 AC 47 ms
64,320 KB
testcase_19 AC 73 ms
76,420 KB
testcase_20 AC 81 ms
78,160 KB
testcase_21 AC 78 ms
77,060 KB
testcase_22 AC 81 ms
76,676 KB
testcase_23 AC 83 ms
76,804 KB
testcase_24 AC 205 ms
96,532 KB
testcase_25 AC 193 ms
102,812 KB
testcase_26 AC 236 ms
105,276 KB
testcase_27 AC 288 ms
129,248 KB
testcase_28 AC 283 ms
114,452 KB
testcase_29 AC 354 ms
127,912 KB
testcase_30 AC 330 ms
128,552 KB
testcase_31 AC 338 ms
128,552 KB
testcase_32 AC 335 ms
128,556 KB
testcase_33 AC 604 ms
110,760 KB
testcase_34 AC 598 ms
110,696 KB
testcase_35 AC 628 ms
110,432 KB
testcase_36 AC 613 ms
110,500 KB
testcase_37 AC 628 ms
110,500 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self,size):
        self.uf=[[-1,0,1] for i in range(size)]
        
    def unite(self,fir,sec):
        one=self.root(fir)
        two=self.root(sec)
        if one!=two:
            if self.uf[one][1]>self.uf[two][1]:
                one,two=two,one
            self.uf[one][0]=two
            self.uf[two][2]+=self.uf[one][2]
            if self.uf[one][1]==self.uf[two][1]:
                self.uf[two][1]+=1
    
    def same(self,fir,sec):
        return self.root(fir)==self.root(sec)
    
    def root(self,node):
        pos=node
        change=[]
        while self.uf[pos][0]!=-1:
            change.append(pos)
            pos=self.uf[pos][0]
        for i in change:
            self.uf[i][0]=pos
        return pos
    
    def size(self,node):
        return self.uf[self.root(node)][2]
N,M=map(int,input().split())
C=list(map(int,input().split()))
union=UnionFind(N)
for i in range(M):
    u,v=map(int,input().split())
    if C[u-1]==C[v-1]:
        union.unite(u-1,v-1)
aft=[set() for i in range(N+1)]
for i in range(N):
    aft[C[i]].add(union.root(i))
ans=0
for i in range(N+1):
    ans+=max(0,len(aft[i])-1)
print(ans)
0