結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,360 KB
testcase_01 AC 75 ms
71,456 KB
testcase_02 AC 75 ms
71,068 KB
testcase_03 AC 137 ms
78,448 KB
testcase_04 AC 126 ms
78,260 KB
testcase_05 AC 134 ms
78,012 KB
testcase_06 AC 129 ms
78,128 KB
testcase_07 AC 127 ms
78,108 KB
testcase_08 AC 128 ms
78,524 KB
testcase_09 AC 145 ms
78,468 KB
testcase_10 AC 77 ms
71,384 KB
testcase_11 AC 76 ms
71,312 KB
testcase_12 AC 76 ms
71,432 KB
testcase_13 AC 76 ms
71,304 KB
testcase_14 AC 76 ms
71,468 KB
testcase_15 AC 76 ms
71,384 KB
testcase_16 AC 256 ms
99,832 KB
testcase_17 AC 366 ms
101,884 KB
testcase_18 AC 242 ms
99,764 KB
testcase_19 AC 521 ms
104,332 KB
testcase_20 AC 486 ms
102,848 KB
testcase_21 AC 484 ms
102,712 KB
testcase_22 AC 366 ms
101,884 KB
testcase_23 AC 376 ms
101,968 KB
testcase_24 AC 378 ms
101,836 KB
testcase_25 AC 500 ms
103,756 KB
testcase_26 AC 487 ms
103,412 KB
testcase_27 AC 490 ms
103,108 KB
testcase_28 AC 488 ms
103,620 KB
testcase_29 AC 505 ms
103,780 KB
testcase_30 AC 495 ms
103,732 KB
testcase_31 AC 479 ms
103,540 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