結果

問題 No.1390 Get together
ユーザー 👑 H20H20
提出日時 2021-02-12 21:54:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 507 ms / 2,000 ms
コード長 1,650 bytes
コンパイル時間 370 ms
コンパイル使用メモリ 87,320 KB
実行使用メモリ 110,668 KB
最終ジャッジ日時 2023-09-27 03:49:29
合計ジャッジ時間 10,845 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,716 KB
testcase_01 AC 92 ms
71,460 KB
testcase_02 AC 94 ms
71,600 KB
testcase_03 AC 174 ms
79,032 KB
testcase_04 AC 137 ms
78,620 KB
testcase_05 AC 139 ms
78,620 KB
testcase_06 AC 136 ms
78,100 KB
testcase_07 AC 136 ms
78,088 KB
testcase_08 AC 138 ms
79,164 KB
testcase_09 AC 145 ms
78,900 KB
testcase_10 AC 94 ms
71,620 KB
testcase_11 AC 94 ms
71,600 KB
testcase_12 AC 94 ms
71,688 KB
testcase_13 AC 95 ms
71,712 KB
testcase_14 AC 95 ms
71,684 KB
testcase_15 AC 93 ms
71,724 KB
testcase_16 AC 253 ms
97,852 KB
testcase_17 AC 375 ms
110,668 KB
testcase_18 AC 241 ms
97,844 KB
testcase_19 AC 483 ms
106,280 KB
testcase_20 AC 459 ms
109,920 KB
testcase_21 AC 507 ms
110,472 KB
testcase_22 AC 364 ms
101,976 KB
testcase_23 AC 369 ms
102,176 KB
testcase_24 AC 370 ms
100,932 KB
testcase_25 AC 452 ms
105,032 KB
testcase_26 AC 449 ms
106,100 KB
testcase_27 AC 476 ms
106,440 KB
testcase_28 AC 456 ms
106,072 KB
testcase_29 AC 452 ms
106,480 KB
testcase_30 AC 444 ms
105,648 KB
testcase_31 AC 457 ms
105,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict

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 = []
for i in range(N+1):
    L.append([])

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

uf = UnionFind(M+1)
for l in L:
    for i in range(1,len(l)):
        uf.union(l[0],l[i])


print(M-uf.group_count()+1)
0