結果

問題 No.1390 Get together
ユーザー Shiro YangShiro Yang
提出日時 2024-01-27 17:13:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 410 ms / 2,000 ms
コード長 1,643 bytes
コンパイル時間 344 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 96,264 KB
最終ジャッジ日時 2024-01-27 17:13:33
合計ジャッジ時間 9,247 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,612 KB
testcase_01 AC 39 ms
55,612 KB
testcase_02 AC 39 ms
55,612 KB
testcase_03 AC 94 ms
76,832 KB
testcase_04 AC 86 ms
76,688 KB
testcase_05 AC 88 ms
76,688 KB
testcase_06 AC 85 ms
76,816 KB
testcase_07 AC 86 ms
76,688 KB
testcase_08 AC 86 ms
76,688 KB
testcase_09 AC 93 ms
76,960 KB
testcase_10 AC 40 ms
55,612 KB
testcase_11 AC 39 ms
55,612 KB
testcase_12 AC 39 ms
55,612 KB
testcase_13 AC 40 ms
55,612 KB
testcase_14 AC 40 ms
55,612 KB
testcase_15 AC 48 ms
55,612 KB
testcase_16 AC 200 ms
93,220 KB
testcase_17 AC 302 ms
94,820 KB
testcase_18 AC 193 ms
92,812 KB
testcase_19 AC 377 ms
96,128 KB
testcase_20 AC 400 ms
96,264 KB
testcase_21 AC 377 ms
96,024 KB
testcase_22 AC 286 ms
94,564 KB
testcase_23 AC 304 ms
95,076 KB
testcase_24 AC 287 ms
95,332 KB
testcase_25 AC 365 ms
95,844 KB
testcase_26 AC 385 ms
96,124 KB
testcase_27 AC 388 ms
95,588 KB
testcase_28 AC 385 ms
95,844 KB
testcase_29 AC 376 ms
95,588 KB
testcase_30 AC 410 ms
95,844 KB
testcase_31 AC 393 ms
96,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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())
G = [[] for _ in range(N+1)]

uf = UnionFind(M+1)

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

cnt = 0

for li in G:
    for i in range(len(li)):
        if i == 0: continue
        if uf.same(li[0], li[i]): continue
        uf.union(li[0], li[i])
        cnt += 1

print(cnt)
0