結果

問題 No.1390 Get together
ユーザー Shiro YangShiro Yang
提出日時 2024-01-27 17:25:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 379 ms / 2,000 ms
コード長 1,139 bytes
コンパイル時間 288 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 98,396 KB
最終ジャッジ日時 2024-01-27 17:25:25
合計ジャッジ時間 8,149 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,460 KB
testcase_01 AC 54 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 97 ms
76,556 KB
testcase_04 AC 81 ms
76,300 KB
testcase_05 AC 94 ms
76,684 KB
testcase_06 AC 81 ms
76,172 KB
testcase_07 AC 88 ms
76,428 KB
testcase_08 AC 81 ms
76,300 KB
testcase_09 AC 96 ms
76,684 KB
testcase_10 AC 37 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
testcase_12 AC 37 ms
53,460 KB
testcase_13 AC 35 ms
53,460 KB
testcase_14 AC 35 ms
53,460 KB
testcase_15 AC 35 ms
53,460 KB
testcase_16 AC 216 ms
94,360 KB
testcase_17 AC 267 ms
98,396 KB
testcase_18 AC 192 ms
94,344 KB
testcase_19 AC 344 ms
96,728 KB
testcase_20 AC 373 ms
96,728 KB
testcase_21 AC 379 ms
97,512 KB
testcase_22 AC 289 ms
96,340 KB
testcase_23 AC 285 ms
96,472 KB
testcase_24 AC 318 ms
96,472 KB
testcase_25 AC 369 ms
97,192 KB
testcase_26 AC 337 ms
96,728 KB
testcase_27 AC 362 ms
96,728 KB
testcase_28 AC 347 ms
96,728 KB
testcase_29 AC 341 ms
96,728 KB
testcase_30 AC 367 ms
97,188 KB
testcase_31 AC 371 ms
96,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = [-1] * n
        self.size = [1] * n
    def find(self, x):
        if self.parent[x] == -1: return x
        self.parent[x] = self.find(self.parent[x])
        return self.parent[x]
    def union(self, x, y):
        px = self.find(x)
        py = self.find(y)
        if px == py: return
        if self.size[px] < self.size[py]: px, py = py, px
        self.parent[py] = px
        self.size[px] += self.size[py]
    def same(self, x, y):
        return self.find(x) == self.find(y)
    def get_size(self, x):
        return self.size[self.find(x)]
    def get_all_groups(self):
        n = len(self.parent)
        groups = [[] for _ in range(n)]
        for i in range(n):
            groups[self.find(i)].append(i)
        return list(filter(lambda x: x, groups))

N, M = map(int, input().split())
G = [[] for _ in range(N+1)]
for _ in range(N):
    b, c = map(int, input().split())
    G[c].append(b)

uf = UnionFind(M+1)
ans = 0
for col in G:
    for box in col:
        if not uf.same(col[0], box):
            uf.union(col[0], box)
            ans += 1
print(ans)
0