結果

問題 No.1390 Get together
ユーザー NoneNone
提出日時 2021-05-04 23:53:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 471 ms / 2,000 ms
コード長 2,553 bytes
コンパイル時間 1,395 ms
コンパイル使用メモリ 86,280 KB
実行使用メモリ 181,484 KB
最終ジャッジ日時 2023-10-01 01:16:04
合計ジャッジ時間 10,801 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
70,988 KB
testcase_01 AC 71 ms
71,084 KB
testcase_02 AC 71 ms
71,024 KB
testcase_03 AC 126 ms
78,520 KB
testcase_04 AC 121 ms
77,968 KB
testcase_05 AC 123 ms
77,744 KB
testcase_06 AC 124 ms
78,348 KB
testcase_07 AC 121 ms
77,936 KB
testcase_08 AC 124 ms
78,116 KB
testcase_09 AC 126 ms
78,148 KB
testcase_10 AC 72 ms
71,172 KB
testcase_11 AC 73 ms
71,380 KB
testcase_12 AC 72 ms
71,100 KB
testcase_13 AC 71 ms
71,216 KB
testcase_14 AC 70 ms
71,232 KB
testcase_15 AC 70 ms
71,232 KB
testcase_16 AC 344 ms
181,484 KB
testcase_17 AC 310 ms
179,544 KB
testcase_18 AC 303 ms
149,320 KB
testcase_19 AC 461 ms
159,304 KB
testcase_20 AC 394 ms
172,708 KB
testcase_21 AC 413 ms
173,560 KB
testcase_22 AC 414 ms
160,960 KB
testcase_23 AC 412 ms
160,456 KB
testcase_24 AC 406 ms
160,660 KB
testcase_25 AC 467 ms
153,832 KB
testcase_26 AC 461 ms
153,332 KB
testcase_27 AC 470 ms
155,924 KB
testcase_28 AC 466 ms
157,300 KB
testcase_29 AC 471 ms
154,316 KB
testcase_30 AC 470 ms
157,240 KB
testcase_31 AC 471 ms
157,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        """ 根を見つける関数を定義(同時にxを直接根にくっつける操作も行う)"""
        tmp = []
        parents = self.parents
        while parents[x] >= 0:
            tmp.append(x)
            x = parents[x]
        for y in tmp:
            parents[y] = x
        return 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 same(self, x, y):
        """ xとyが同じ根の子かを判定 """
        return self.find(x) == self.find(y)

    def size(self, x):
        """ xの根のparent(= 要素数)を返す """
        return -self.parents[self.find(x)]

    def members(self, x):
        """ xが属するグループの要素をリストとして返す O(N)"""
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        """ 全ての根の要素をリストとして返す O(N)"""
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ グループの数を返す O(N)"""
        return len(self.roots())

    def size_list(self):
        """ 各グループの要素数のリストを返す(根の番号は返さない) O(N)"""
        return [-x for x in self.parents if x < 0]

    def all_group_members(self):
        """ {根:[根の子(根を含む)のリスト],...}を辞書で返す O(N)"""
        res = [[] for _ in range(self.n)]
        for i in range(self.n):
            x = self.find(i)
            res[x].append(i)
        return {r: res[r] for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

##############################################################################################################
import sys
input = sys.stdin.readline

N, M = map(int, input().split())
U = UnionFind(N+M)
for _ in range(N):
    b,c = map(int, input().split())
    b,c = b-1, c-1
    U.union(N+b, c)

res=0
for k,v in U.all_group_members().items():
    cnt=0
    for a in v:
        if a>=N:
            cnt+=1
    res+=max(cnt-1,0)

print(res)
0