結果

問題 No.1390 Get together
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-02-12 22:04:57
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,146 bytes
コンパイル時間 811 ms
コンパイル使用メモリ 11,032 KB
実行使用メモリ 81,520 KB
最終ジャッジ日時 2023-09-27 04:07:31
合計ジャッジ時間 23,592 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,380 KB
testcase_01 AC 15 ms
8,248 KB
testcase_02 AC 16 ms
8,444 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 15 ms
8,376 KB
testcase_15 AC 16 ms
8,240 KB
testcase_16 AC 1,241 ms
79,808 KB
testcase_17 AC 1,086 ms
81,296 KB
testcase_18 AC 1,285 ms
79,588 KB
testcase_19 AC 1,228 ms
81,432 KB
testcase_20 AC 1,177 ms
81,388 KB
testcase_21 AC 1,151 ms
81,336 KB
testcase_22 AC 1,213 ms
80,852 KB
testcase_23 AC 1,264 ms
80,928 KB
testcase_24 AC 1,261 ms
80,852 KB
testcase_25 AC 1,246 ms
81,400 KB
testcase_26 AC 1,243 ms
81,396 KB
testcase_27 AC 1,225 ms
81,456 KB
testcase_28 AC 1,215 ms
81,452 KB
testcase_29 AC 1,224 ms
81,300 KB
testcase_30 AC 1,236 ms
81,520 KB
testcase_31 AC 1,247 ms
81,384 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(n)
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