結果

問題 No.1390 Get together
ユーザー 👑 tatyamtatyam
提出日時 2020-09-06 10:37:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 326 ms / 2,000 ms
コード長 1,130 bytes
コンパイル時間 163 ms
コンパイル使用メモリ 81,988 KB
実行使用メモリ 80,640 KB
最終ジャッジ日時 2024-05-07 16:11:06
合計ジャッジ時間 7,133 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,096 KB
testcase_01 AC 38 ms
52,096 KB
testcase_02 AC 37 ms
51,968 KB
testcase_03 AC 99 ms
76,196 KB
testcase_04 AC 96 ms
76,216 KB
testcase_05 AC 108 ms
76,416 KB
testcase_06 AC 94 ms
76,336 KB
testcase_07 AC 94 ms
76,800 KB
testcase_08 AC 97 ms
76,356 KB
testcase_09 AC 101 ms
76,520 KB
testcase_10 AC 38 ms
52,608 KB
testcase_11 AC 37 ms
52,224 KB
testcase_12 AC 38 ms
51,968 KB
testcase_13 AC 38 ms
51,840 KB
testcase_14 AC 39 ms
52,480 KB
testcase_15 AC 39 ms
51,840 KB
testcase_16 AC 204 ms
79,744 KB
testcase_17 AC 209 ms
79,232 KB
testcase_18 AC 194 ms
79,744 KB
testcase_19 AC 305 ms
79,872 KB
testcase_20 AC 323 ms
80,076 KB
testcase_21 AC 326 ms
80,000 KB
testcase_22 AC 254 ms
79,680 KB
testcase_23 AC 242 ms
79,796 KB
testcase_24 AC 242 ms
79,872 KB
testcase_25 AC 283 ms
79,616 KB
testcase_26 AC 276 ms
80,216 KB
testcase_27 AC 274 ms
80,012 KB
testcase_28 AC 286 ms
80,076 KB
testcase_29 AC 274 ms
79,744 KB
testcase_30 AC 274 ms
79,840 KB
testcase_31 AC 276 ms
80,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, size):
        self.data = [-1] * size
    def root(self, x):
        if self.data[x] < 0:
            return x
        ans = self.root(self.data[x])
        self.data[x] = ans
        return ans
    def unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False
        if self.data[x] > self.data[y]:
            x, y = y, x
        self.data[x] += self.data[y]
        self.data[y] = x
        return True
    def is_root(self, x):
        return self.data[x] < 0
    def size(self, x):
        return -self.data[self.root(x)]

n, m = map(int, input().split(' '))
assert 1 <= n <= 200000
assert 1 <= m <= 200000

uf = UnionFind(n + m) # n + i に色 i の超頂点をおく
for i in range(n):
    b, c = map(int, input().split(' '))
    assert 1 <= b <= m
    assert 1 <= c <= n
    b -= 1
    c -= 1
    uf.unite(b, m + c)

ans = 0
for i in range(n + m):
    if uf.is_root(i):
        ans += uf.size(i) - 1
for i in range(m, m + n): # 超頂点の分足しすぎるので、引く
    if uf.size(i) > 1:
        ans -= 1

print(ans)
0