結果

問題 No.1390 Get together
ユーザー lloyzlloyz
提出日時 2022-03-20 00:20:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 660 ms / 2,000 ms
コード長 2,231 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 124,388 KB
最終ジャッジ日時 2024-04-15 14:48:13
合計ジャッジ時間 13,196 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
54,016 KB
testcase_01 AC 50 ms
54,016 KB
testcase_02 AC 49 ms
53,760 KB
testcase_03 AC 135 ms
78,336 KB
testcase_04 AC 116 ms
77,568 KB
testcase_05 AC 127 ms
77,824 KB
testcase_06 AC 119 ms
77,440 KB
testcase_07 AC 119 ms
77,440 KB
testcase_08 AC 116 ms
77,440 KB
testcase_09 AC 141 ms
78,208 KB
testcase_10 AC 49 ms
54,272 KB
testcase_11 AC 48 ms
53,760 KB
testcase_12 AC 48 ms
54,144 KB
testcase_13 AC 47 ms
53,760 KB
testcase_14 AC 48 ms
54,016 KB
testcase_15 AC 48 ms
54,016 KB
testcase_16 AC 366 ms
108,224 KB
testcase_17 AC 353 ms
107,808 KB
testcase_18 AC 311 ms
120,876 KB
testcase_19 AC 616 ms
124,388 KB
testcase_20 AC 660 ms
110,068 KB
testcase_21 AC 645 ms
111,048 KB
testcase_22 AC 500 ms
121,044 KB
testcase_23 AC 513 ms
121,048 KB
testcase_24 AC 539 ms
122,764 KB
testcase_25 AC 652 ms
123,236 KB
testcase_26 AC 655 ms
123,280 KB
testcase_27 AC 645 ms
123,372 KB
testcase_28 AC 652 ms
123,016 KB
testcase_29 AC 626 ms
122,964 KB
testcase_30 AC 650 ms
123,136 KB
testcase_31 AC 642 ms
123,264 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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())

n, m = map(int, input().split())
B = defaultdict(list)
C = defaultdict(list)
for i in range(n):
    b, c = map(int, input().split())
    B[b].append(i)
    C[c].append(i)

UF = UnionFind(n)
for BB in B.values():
    f = BB[0]
    for j in BB[1:]:
        UF.union(f, j)

ans = 0
for CC in C.values():
    f = CC[0]
    for j in CC[1:]:
        if UF.same(f, j):
            continue
        ans += 1
        UF.union(f, j)
print(ans)
0