結果

問題 No.2563 色ごとのグループ
ユーザー tkykwtnbtkykwtnb
提出日時 2024-04-03 21:09:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,458 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 95,200 KB
最終ジャッジ日時 2024-04-03 21:10:21
合計ジャッジ時間 30,254 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,112 KB
testcase_01 AC 30 ms
10,112 KB
testcase_02 AC 29 ms
10,112 KB
testcase_03 AC 28 ms
10,112 KB
testcase_04 AC 29 ms
10,112 KB
testcase_05 AC 29 ms
10,112 KB
testcase_06 AC 29 ms
10,112 KB
testcase_07 AC 29 ms
10,112 KB
testcase_08 AC 28 ms
10,112 KB
testcase_09 AC 31 ms
10,112 KB
testcase_10 WA -
testcase_11 AC 30 ms
10,112 KB
testcase_12 WA -
testcase_13 AC 29 ms
10,112 KB
testcase_14 AC 37 ms
10,624 KB
testcase_15 AC 40 ms
10,624 KB
testcase_16 AC 40 ms
10,624 KB
testcase_17 AC 39 ms
10,624 KB
testcase_18 AC 32 ms
10,624 KB
testcase_19 AC 88 ms
11,136 KB
testcase_20 WA -
testcase_21 AC 83 ms
13,568 KB
testcase_22 AC 85 ms
12,416 KB
testcase_23 AC 151 ms
13,824 KB
testcase_24 AC 1,576 ms
49,104 KB
testcase_25 AC 946 ms
49,928 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 1,537 ms
75,692 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent_size = [-1] * n
    def leader(self, a):
        if self.parent_size[a] < 0: return a
        self.parent_size[a] = self.leader(self.parent_size[a])
        return self.parent_size[a]
    def merge(self, a, b):
        x, y = self.leader(a), self.leader(b)
        if x == y: return
        if abs(self.parent_size[x]) < abs(self.parent_size[y]): x, y = y, x
        self.parent_size[x] += self.parent_size[y]
        self.parent_size[y] = x
        return
    def same(self, a, b):
        return self.leader(a) == self.leader(b)
    def size(self, a):
        return abs(self.parent_size[self.leader(a)])
    def groups(self):
        result=[[] for _ in range(self.n)]
        for i in range(self.n):
            result[self.leader(i)].append(i)
        return [r for r in result if r != []]

N,M=map(int,input().split())
C=list(map(int,input().split()))
G=[[] for _ in range(N)]
for _ in range(M):
    u,v=map(int,input().split())
    u-=1;v-=1
    G[u].append(v)
    G[v].append(u)

uf=UnionFind(N)
vis=[0 for _ in range(N)]
from collections import deque,defaultdict
Q=deque()
Q.append(0)
while Q:
    cur=Q.popleft()
    vis[cur]=1
    for nxt in G[cur]:
        if C[cur]==C[nxt]:uf.merge(cur,nxt)
        if vis[nxt]==0:Q.append(nxt)
D=defaultdict(set)
for i in range(N):
    L=uf.leader(i)
    D[C[L]].add(L)
ans=0
for k,v in D.items():
    ans+=len(v)-1
print(ans)
0