結果

問題 No.1390 Get together
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-02-12 22:06:03
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 513 ms / 2,000 ms
コード長 1,146 bytes
コンパイル時間 266 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 124,756 KB
最終ジャッジ日時 2023-09-27 04:09:49
合計ジャッジ時間 10,136 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,216 KB
testcase_01 AC 68 ms
71,296 KB
testcase_02 AC 68 ms
71,388 KB
testcase_03 AC 119 ms
78,268 KB
testcase_04 AC 128 ms
78,512 KB
testcase_05 AC 125 ms
78,936 KB
testcase_06 AC 121 ms
78,668 KB
testcase_07 AC 118 ms
78,640 KB
testcase_08 AC 118 ms
78,748 KB
testcase_09 AC 125 ms
78,932 KB
testcase_10 AC 69 ms
71,396 KB
testcase_11 AC 69 ms
71,440 KB
testcase_12 AC 69 ms
71,260 KB
testcase_13 AC 68 ms
71,240 KB
testcase_14 AC 69 ms
71,520 KB
testcase_15 AC 67 ms
71,104 KB
testcase_16 AC 330 ms
124,756 KB
testcase_17 AC 341 ms
118,784 KB
testcase_18 AC 328 ms
124,020 KB
testcase_19 AC 453 ms
120,208 KB
testcase_20 AC 441 ms
120,672 KB
testcase_21 AC 513 ms
121,236 KB
testcase_22 AC 396 ms
120,100 KB
testcase_23 AC 394 ms
121,040 KB
testcase_24 AC 387 ms
121,108 KB
testcase_25 AC 452 ms
119,920 KB
testcase_26 AC 449 ms
120,280 KB
testcase_27 AC 451 ms
120,264 KB
testcase_28 AC 437 ms
120,544 KB
testcase_29 AC 436 ms
120,172 KB
testcase_30 AC 430 ms
120,124 KB
testcase_31 AC 444 ms
119,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.par = list(range(self.n))
        self.rank = [1] * n
        self.count = n

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def unite(self, x, y):
        p = self.find(x)
        q = self.find(y)
        if p == q:
            return None
        if p > q:
            p, q = q, p
        self.rank[p] += self.rank[q]
        self.par[q] = p
        self.count -= 1

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def size(self, x):
        return self.rank[x]

    def count(self):
        return self.count
n, m = map(int, input().split())
UF = UnionFind(m)
bc = [list(map(int, input().split())) for i in range(n)]
d = [[] for i in range(n)]
for b, c in bc:
    d[c - 1].append(b - 1)
for i in range(n):
    d[i].sort()
ans = 0
for i in range(n):
    for j in range(len(d[i]) - 1):
        if UF.same(d[i][j], d[i][j + 1]):
            continue
        UF.unite(d[i][j], d[i][j + 1])
        ans += 1
print(ans)
0