結果

問題 No.1390 Get together
ユーザー moshi777moshi777
提出日時 2021-06-17 16:14:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 1,614 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 86,868 KB
実行使用メモリ 102,776 KB
最終ジャッジ日時 2023-08-30 23:33:07
合計ジャッジ時間 14,993 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 160 ms
80,648 KB
testcase_01 AC 160 ms
80,876 KB
testcase_02 AC 161 ms
80,828 KB
testcase_03 AC 222 ms
83,840 KB
testcase_04 AC 209 ms
83,700 KB
testcase_05 AC 229 ms
84,312 KB
testcase_06 AC 207 ms
83,604 KB
testcase_07 AC 206 ms
83,776 KB
testcase_08 AC 203 ms
83,964 KB
testcase_09 AC 209 ms
83,560 KB
testcase_10 AC 163 ms
80,892 KB
testcase_11 AC 162 ms
80,656 KB
testcase_12 AC 162 ms
81,040 KB
testcase_13 AC 164 ms
80,964 KB
testcase_14 AC 167 ms
81,016 KB
testcase_15 AC 158 ms
81,008 KB
testcase_16 AC 308 ms
97,368 KB
testcase_17 AC 383 ms
102,776 KB
testcase_18 AC 296 ms
97,208 KB
testcase_19 AC 461 ms
102,080 KB
testcase_20 AC 478 ms
102,272 KB
testcase_21 AC 517 ms
102,652 KB
testcase_22 AC 408 ms
100,884 KB
testcase_23 AC 417 ms
101,004 KB
testcase_24 AC 415 ms
100,968 KB
testcase_25 AC 498 ms
101,812 KB
testcase_26 AC 460 ms
101,628 KB
testcase_27 AC 458 ms
101,620 KB
testcase_28 AC 471 ms
101,796 KB
testcase_29 AC 458 ms
101,680 KB
testcase_30 AC 497 ms
102,532 KB
testcase_31 AC 466 ms
101,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from typing import Union


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())


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

ans = 0
uf = UnionFind(M+1)
for l in L:
    for b in l:
        if not uf.same(l[0], b):
            uf.union(l[0], b)
            ans += 1

print(ans)
0