結果

問題 No.2563 色ごとのグループ
ユーザー hirayuu_ychirayuu_yc
提出日時 2023-12-02 15:10:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 567 ms / 2,000 ms
コード長 1,175 bytes
コンパイル時間 460 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 129,720 KB
最終ジャッジ日時 2024-09-26 18:13:14
合計ジャッジ時間 8,641 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
52,608 KB
testcase_01 AC 37 ms
52,864 KB
testcase_02 AC 37 ms
52,144 KB
testcase_03 AC 35 ms
52,480 KB
testcase_04 AC 36 ms
52,096 KB
testcase_05 AC 37 ms
52,864 KB
testcase_06 AC 37 ms
52,864 KB
testcase_07 AC 37 ms
52,736 KB
testcase_08 AC 36 ms
52,608 KB
testcase_09 AC 40 ms
53,632 KB
testcase_10 AC 39 ms
52,480 KB
testcase_11 AC 40 ms
53,248 KB
testcase_12 AC 38 ms
53,120 KB
testcase_13 AC 39 ms
52,752 KB
testcase_14 AC 70 ms
72,320 KB
testcase_15 AC 69 ms
72,704 KB
testcase_16 AC 67 ms
72,192 KB
testcase_17 AC 67 ms
72,064 KB
testcase_18 AC 51 ms
64,552 KB
testcase_19 AC 76 ms
76,680 KB
testcase_20 AC 87 ms
78,592 KB
testcase_21 AC 82 ms
77,284 KB
testcase_22 AC 78 ms
77,464 KB
testcase_23 AC 85 ms
77,440 KB
testcase_24 AC 198 ms
97,084 KB
testcase_25 AC 191 ms
102,968 KB
testcase_26 AC 234 ms
105,304 KB
testcase_27 AC 263 ms
129,720 KB
testcase_28 AC 262 ms
115,136 KB
testcase_29 AC 321 ms
128,348 KB
testcase_30 AC 335 ms
128,732 KB
testcase_31 AC 338 ms
128,600 KB
testcase_32 AC 346 ms
128,840 KB
testcase_33 AC 549 ms
110,972 KB
testcase_34 AC 547 ms
110,756 KB
testcase_35 AC 560 ms
110,568 KB
testcase_36 AC 567 ms
110,540 KB
testcase_37 AC 555 ms
110,660 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