結果

問題 No.2563 色ごとのグループ
ユーザー moharan627moharan627
提出日時 2023-12-02 15:06:02
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,358 bytes
コンパイル時間 379 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 218,508 KB
最終ジャッジ日時 2023-12-02 15:06:10
合計ジャッジ時間 6,458 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,612 KB
testcase_01 AC 42 ms
55,612 KB
testcase_02 AC 41 ms
55,612 KB
testcase_03 AC 41 ms
55,612 KB
testcase_04 AC 42 ms
55,612 KB
testcase_05 AC 41 ms
55,612 KB
testcase_06 AC 41 ms
55,612 KB
testcase_07 AC 41 ms
55,612 KB
testcase_08 AC 42 ms
55,612 KB
testcase_09 AC 47 ms
63,376 KB
testcase_10 AC 45 ms
61,292 KB
testcase_11 AC 47 ms
63,376 KB
testcase_12 AC 46 ms
63,376 KB
testcase_13 AC 45 ms
61,276 KB
testcase_14 AC 96 ms
76,396 KB
testcase_15 AC 97 ms
76,652 KB
testcase_16 AC 89 ms
76,652 KB
testcase_17 AC 87 ms
76,268 KB
testcase_18 AC 108 ms
76,616 KB
testcase_19 AC 97 ms
76,524 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10**9 + 7
MOD2 = 998244353
from collections import defaultdict
def ceil(A,B):
    return -(-A//B)
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
def solve():
    def II(): return int(sys.stdin.readline())
    def LI(): return list(map(int, sys.stdin.readline().split()))
    def LC(): return list(sys.stdin.readline().rstrip())
    def IC(): return [int(c) for c in sys.stdin.readline().rstrip()]
    def MI(): return map(int, sys.stdin.readline().split())
    N,M = MI()
    C = LI()
    Num = defaultdict(lambda:0)
    for c in C:
        Num[c] += 1
    Edge = [0]*M
    E = defaultdict(lambda:[])
    for m in range(M):
        U,V = MI()
        U-=1
        V-=1
        if(C[U]==C[V]):
            E[C[U]].append((U,V))
    uf = UnionFind(N)
    ans = 0
    for c in range(1,N+1):
        if(0<Num[c]):
            Pre = uf.group_count()
            if(c in E):
                for u,v in E[c]:
                    uf.union(u,v)
            Nex = uf.group_count()
            #print(Num[c],Pre,Nex)
            ans += Num[c] - (Pre-Nex) - 1
    print(ans)
    return
solve()
0