結果

問題 No.2563 色ごとのグループ
ユーザー ThetaTheta
提出日時 2023-12-15 15:41:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,761 ms / 2,000 ms
コード長 2,251 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 124,452 KB
最終ジャッジ日時 2023-12-15 15:41:51
合計ジャッジ時間 23,207 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,240 KB
testcase_01 AC 28 ms
10,240 KB
testcase_02 AC 28 ms
10,240 KB
testcase_03 AC 30 ms
10,240 KB
testcase_04 AC 27 ms
10,240 KB
testcase_05 AC 28 ms
10,240 KB
testcase_06 AC 28 ms
10,240 KB
testcase_07 AC 29 ms
10,240 KB
testcase_08 AC 28 ms
10,240 KB
testcase_09 AC 29 ms
10,368 KB
testcase_10 AC 27 ms
10,240 KB
testcase_11 AC 28 ms
10,368 KB
testcase_12 AC 30 ms
10,368 KB
testcase_13 AC 28 ms
10,240 KB
testcase_14 AC 36 ms
11,008 KB
testcase_15 AC 37 ms
11,136 KB
testcase_16 AC 37 ms
11,136 KB
testcase_17 AC 36 ms
11,008 KB
testcase_18 AC 33 ms
11,136 KB
testcase_19 AC 55 ms
11,904 KB
testcase_20 AC 112 ms
18,944 KB
testcase_21 AC 78 ms
14,848 KB
testcase_22 AC 70 ms
13,824 KB
testcase_23 AC 106 ms
16,512 KB
testcase_24 AC 896 ms
70,240 KB
testcase_25 AC 753 ms
63,968 KB
testcase_26 AC 1,059 ms
85,248 KB
testcase_27 AC 1,041 ms
105,444 KB
testcase_28 AC 1,198 ms
97,144 KB
testcase_29 AC 1,647 ms
123,084 KB
testcase_30 AC 1,627 ms
123,132 KB
testcase_31 AC 1,625 ms
123,144 KB
testcase_32 AC 1,625 ms
123,124 KB
testcase_33 AC 1,761 ms
124,412 KB
testcase_34 AC 1,717 ms
124,276 KB
testcase_35 AC 1,754 ms
124,304 KB
testcase_36 AC 1,757 ms
124,408 KB
testcase_37 AC 1,710 ms
124,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
sys.setrecursionlimit(20000)


class UnionFindTree:
    def __init__(self, length: int) -> None:
        self.list = [idx for idx in range(length)]
        self.size = [1 for _ in range(length)]

    def get_root(self, idx: int) -> int:
        if self.list[idx] == idx:
            return idx
        self.list[idx] = self.get_root(self.list[idx])
        return self.list[idx]

    def is_same(self, idx1: int, idx2: int) -> bool:
        return self.get_root(idx1) == self.get_root(idx2)

    def merge(self, idx1: int, idx2: int):
        if self.is_same(idx1, idx2):
            return

        idx1_root = self.get_root(idx1)
        idx2_root = self.get_root(idx2)
        if self.size[idx1_root] > self.size[idx2_root]:
            self.list[idx2_root] = idx1_root
            self.size[idx1_root] += self.size[idx2_root]
        else:
            self.list[idx1_root] = idx2_root
            self.size[idx2_root] += self.size[idx1_root]

    def get_size(self, idx: int) -> int:
        return self.size[self.get_root(idx)]


def main():
    N, M = map(int, input().split())
    C = list(map(int, input().split()))
    graph: list[set[int]] = [set() for _ in range(N)]
    for _ in range(M):
        dep, dest = map(lambda n: int(n) - 1, input().split())
        graph[dep].add(dest)
        graph[dest].add(dep)

    color_node: dict[int, set[int]] = defaultdict(set)
    for node_idx, color in enumerate(C):
        color_node[color].add(node_idx)

    add_edge_ctr = 0
    for current_color, nodes in color_node.items():
        node_comp = {node_idx: idx for idx, node_idx in enumerate(nodes)}
        uft = UnionFindTree(len(nodes))
        for node_idx, comp_idx in node_comp.items():
            for neighbor_node_idx in graph[node_idx]:
                if neighbor_node_idx not in nodes:
                    continue
                uft.merge(comp_idx, node_comp[neighbor_node_idx])

        for node_idx, comp_idx in node_comp.items():
            if comp_idx == 0:
                continue
            if uft.is_same(comp_idx, 0):
                continue
            uft.merge(comp_idx, 0)
            add_edge_ctr += 1
    print(add_edge_ctr)


if __name__ == "__main__":
    main()
0