結果

問題 No.1390 Get together
コンテスト
ユーザー Kohei
提出日時 2026-10-13 21:41:03
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 421 ms / 2,000 ms
+ 478µs
コード長 2,957 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 89 ms
コンパイル使用メモリ 65,020 KB
実行使用メモリ 106,560 KB
最終ジャッジ日時 2026-10-13 21:41:19
合計ジャッジ時間 13,188 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.link = [i for i in range(n)]
        #self.L = list(range(n))
        #self.R = list(range(n))
        self.score = [0] * 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):
        rootx = self.find(x)
        rooty = self.find(y)

        if rootx == rooty: # すでに same ならなにもしない!
            return

        if self.parents[rootx] > self.parents[rooty]: # -3 > -4 のようなケース(前者の方が軍勢が少ない)
            rootx, rooty = rooty, rootx # rootx = 多い, rooty = 少ない の順番に変更

        # 統合(rootx が新しい棟梁, rooty は軍門に下った)
        self.parents[rootx] += self.parents[rooty] # 多い方(rootx)に併合する
        self.parents[rooty] = rootx

        self.link[x], self.link[y] = self.link[y], self.link[x]
        #self.L[rootx] = min(self.L[rootx], self.L[rooty])
        #self.R[rootx] = max(self.R[rootx], self.R[rooty])

        self.score[rootx] += self.score[rooty]
        self.score[rooty] = 0

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

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

    def members(self, x): # O(K: 集合の要素数) 最小で1、最大でN
        res = [x]
        now = self.link[x]
        while now != x:
            res.append(now)
            now = self.link[now]
        return res

    #def get_LR(self, x):
    #    x = self.find(x)
    #    return (self.L[x], self.R[x])

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

    def group_count(self): # roots() の呼び出しは全頂点の par をチェックする=遅いため、N回使うと TLE する
        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 main():
    N, M = map(int, input().split())
    data = [set() for _ in range(N)]
    # data[color] = {箱1,箱3}
    uf = UnionFind(M)
    for i in range(N):
        b, c = map(int, input().split())
        b -= 1
        c -= 1
        data[c].add(b)

    ans = 0
    for i in range(N):
        # data[i]
        L = len(data[i])
        boxes = list(data[i])
        for i in range(L - 1):
            a, b = boxes[i], boxes[i + 1]
            if uf.same(a, b):
                continue
            uf.union(a, b)
            ans += 1
    print(ans)
if __name__ == "__main__":
    main()
0