結果

問題 No.1390 Get together
ユーザー rei60rei60
提出日時 2021-02-20 16:31:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 582 ms / 2,000 ms
コード長 1,469 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 112,472 KB
最終ジャッジ日時 2023-10-17 23:36:20
合計ジャッジ時間 13,299 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,512 KB
testcase_01 AC 38 ms
53,512 KB
testcase_02 AC 39 ms
53,512 KB
testcase_03 AC 68 ms
72,720 KB
testcase_04 AC 66 ms
72,756 KB
testcase_05 AC 63 ms
72,736 KB
testcase_06 AC 65 ms
72,752 KB
testcase_07 AC 63 ms
70,668 KB
testcase_08 AC 62 ms
70,660 KB
testcase_09 AC 67 ms
72,720 KB
testcase_10 AC 38 ms
53,512 KB
testcase_11 AC 38 ms
53,512 KB
testcase_12 AC 39 ms
53,512 KB
testcase_13 AC 38 ms
53,512 KB
testcase_14 AC 37 ms
53,512 KB
testcase_15 AC 38 ms
53,512 KB
testcase_16 AC 203 ms
102,612 KB
testcase_17 AC 493 ms
112,252 KB
testcase_18 AC 198 ms
96,300 KB
testcase_19 AC 582 ms
108,612 KB
testcase_20 AC 560 ms
112,448 KB
testcase_21 AC 564 ms
112,472 KB
testcase_22 AC 394 ms
104,340 KB
testcase_23 AC 447 ms
104,256 KB
testcase_24 AC 421 ms
104,900 KB
testcase_25 AC 551 ms
108,336 KB
testcase_26 AC 529 ms
108,448 KB
testcase_27 AC 547 ms
108,440 KB
testcase_28 AC 549 ms
108,756 KB
testcase_29 AC 539 ms
108,372 KB
testcase_30 AC 554 ms
108,372 KB
testcase_31 AC 548 ms
108,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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):
        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):
        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):
        return {r: self.members(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 = lambda: sys.stdin.readline().rstrip()

N, M = map(int,input().split())
uf = UnionFind(M)
BC = [tuple(map(int,input().split())) for i in range(N)]
BC.sort(key=lambda x: x[1])
b,c = 0,0
for bc in BC:
    bb, cc = b, c
    b, c = bc    
    if c == cc:
        uf.union(b-1,bb-1)
print(M-uf.group_count())
0