結果

問題 No.1390 Get together
ユーザー chineristACchineristAC
提出日時 2020-12-14 16:38:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 377 ms / 2,000 ms
コード長 1,288 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 81,744 KB
実行使用メモリ 85,052 KB
最終ジャッジ日時 2023-10-20 05:05:49
合計ジャッジ時間 9,340 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
53,344 KB
testcase_01 AC 40 ms
53,344 KB
testcase_02 AC 42 ms
53,344 KB
testcase_03 AC 95 ms
76,132 KB
testcase_04 AC 89 ms
76,172 KB
testcase_05 AC 97 ms
76,344 KB
testcase_06 AC 87 ms
76,148 KB
testcase_07 AC 85 ms
76,200 KB
testcase_08 AC 96 ms
76,224 KB
testcase_09 AC 95 ms
76,144 KB
testcase_10 AC 39 ms
53,344 KB
testcase_11 AC 39 ms
53,344 KB
testcase_12 AC 40 ms
53,344 KB
testcase_13 AC 40 ms
53,344 KB
testcase_14 AC 40 ms
53,344 KB
testcase_15 AC 40 ms
53,344 KB
testcase_16 AC 193 ms
81,968 KB
testcase_17 AC 182 ms
85,052 KB
testcase_18 AC 184 ms
81,896 KB
testcase_19 AC 358 ms
83,996 KB
testcase_20 AC 368 ms
83,568 KB
testcase_21 AC 377 ms
83,572 KB
testcase_22 AC 278 ms
82,900 KB
testcase_23 AC 282 ms
82,692 KB
testcase_24 AC 282 ms
82,700 KB
testcase_25 AC 349 ms
84,084 KB
testcase_26 AC 358 ms
83,660 KB
testcase_27 AC 348 ms
83,296 KB
testcase_28 AC 342 ms
83,160 KB
testcase_29 AC 349 ms
83,468 KB
testcase_30 AC 345 ms
83,472 KB
testcase_31 AC 350 ms
83,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        stack = [x]
        while self._parent[stack[-1]]!=stack[-1]:
            stack.append(self._parent[stack[-1]])
        for v in stack:
            self._parent[v] = stack[-1]
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

import sys

input = sys.stdin.readline

N,M = map(int,input().split())
uf = UnionFindVerSize(N+M)
for _ in range(N):
    b,c = map(int,input().split())
    uf.unite(b-1,c-1+M)

g = uf.group

for i in range(M,N+M):
    if uf.get_size(i)==1:
        g -= 1

print(M-g)
0