結果

問題 No.1390 Get together
ユーザー 👑 rin204rin204
提出日時 2022-01-18 05:34:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 1,651 bytes
コンパイル時間 390 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 153,344 KB
最終ジャッジ日時 2024-05-02 19:17:59
合計ジャッジ時間 9,598 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,096 KB
testcase_01 AC 37 ms
52,224 KB
testcase_02 AC 33 ms
52,096 KB
testcase_03 AC 92 ms
76,800 KB
testcase_04 AC 90 ms
76,928 KB
testcase_05 AC 94 ms
76,928 KB
testcase_06 AC 95 ms
76,800 KB
testcase_07 AC 92 ms
76,752 KB
testcase_08 AC 92 ms
77,184 KB
testcase_09 AC 99 ms
76,928 KB
testcase_10 AC 35 ms
52,224 KB
testcase_11 AC 34 ms
52,224 KB
testcase_12 AC 34 ms
52,608 KB
testcase_13 AC 34 ms
52,480 KB
testcase_14 AC 34 ms
52,352 KB
testcase_15 AC 34 ms
52,224 KB
testcase_16 AC 215 ms
113,316 KB
testcase_17 AC 320 ms
153,344 KB
testcase_18 AC 189 ms
108,084 KB
testcase_19 AC 416 ms
129,712 KB
testcase_20 AC 414 ms
142,740 KB
testcase_21 AC 407 ms
147,468 KB
testcase_22 AC 296 ms
115,908 KB
testcase_23 AC 315 ms
115,680 KB
testcase_24 AC 298 ms
116,164 KB
testcase_25 AC 387 ms
122,172 KB
testcase_26 AC 373 ms
122,284 KB
testcase_27 AC 360 ms
115,192 KB
testcase_28 AC 368 ms
118,484 KB
testcase_29 AC 377 ms
124,732 KB
testcase_30 AC 361 ms
124,952 KB
testcase_31 AC 368 ms
115,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = 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
        self.group -= 1
        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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
        
n, m = map(int, input().split())
color = [[] for _ in range(n)]
for _ in range(n):
    b, c = map(int, input().split())
    color[c - 1].append(b - 1)
    
UF = UnionFind(m)
for lst in color:
    if not lst:
        continue
    i = lst[0]
    for j in lst[1:]:
        UF.union(i, j)
        
ans = 0
for g in UF.all_group_members().values():
    ans += len(g) - 1
print(ans)
    
0