結果

問題 No.1390 Get together
ユーザー hir355hir355
提出日時 2021-02-12 22:42:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 400 ms / 2,000 ms
コード長 1,248 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 87,240 KB
実行使用メモリ 104,356 KB
最終ジャッジ日時 2023-09-27 05:32:38
合計ジャッジ時間 8,886 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
71,468 KB
testcase_01 AC 61 ms
71,652 KB
testcase_02 AC 62 ms
71,340 KB
testcase_03 AC 111 ms
78,596 KB
testcase_04 AC 107 ms
78,156 KB
testcase_05 AC 110 ms
78,236 KB
testcase_06 AC 106 ms
78,088 KB
testcase_07 AC 107 ms
78,220 KB
testcase_08 AC 105 ms
78,580 KB
testcase_09 AC 112 ms
78,504 KB
testcase_10 AC 63 ms
71,484 KB
testcase_11 AC 62 ms
71,488 KB
testcase_12 AC 62 ms
71,088 KB
testcase_13 AC 60 ms
71,276 KB
testcase_14 AC 63 ms
71,272 KB
testcase_15 AC 63 ms
71,524 KB
testcase_16 AC 220 ms
99,800 KB
testcase_17 AC 273 ms
101,584 KB
testcase_18 AC 209 ms
99,768 KB
testcase_19 AC 393 ms
104,356 KB
testcase_20 AC 385 ms
103,096 KB
testcase_21 AC 400 ms
102,756 KB
testcase_22 AC 297 ms
101,984 KB
testcase_23 AC 299 ms
102,084 KB
testcase_24 AC 306 ms
101,892 KB
testcase_25 AC 383 ms
103,736 KB
testcase_26 AC 381 ms
103,436 KB
testcase_27 AC 389 ms
103,196 KB
testcase_28 AC 383 ms
103,644 KB
testcase_29 AC 388 ms
103,852 KB
testcase_30 AC 381 ms
103,488 KB
testcase_31 AC 378 ms
103,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n + 1)
        self.size = [1] * (n + 1)
 
    # 検索
    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):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
            self.size[y] += self.size[x]
        else:
            self.par[y] = x
            self.size[x] += self.size[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
 
    # 同じ集合に属するか判定
    def same_check(self, x, y):
        return self.find(x) == self.find(y)

n, m = map(int, input().split())
a = [[] for _ in range(n)]
uf = UnionFind(m)
for i in range(n):
    b, c = map(int, input().split())
    a[c - 1].append(b - 1)
for i in range(n):
    for j in range(len(a[i]) - 1):
        uf.unite(a[i][j], a[i][j + 1])
d = [0] * m
ans = 0
for i in range(m):
    i = uf.find(i)
    if d[i]:
        continue
    ans += uf.size[i] - 1
    d[i] = 1
print(ans)
0