結果

問題 No.1390 Get together
ユーザー hiragnhiragn
提出日時 2022-11-29 18:26:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 980 ms / 2,000 ms
コード長 2,307 bytes
コンパイル時間 234 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 55,660 KB
最終ジャッジ日時 2024-10-07 06:09:18
合計ジャッジ時間 17,651 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
10,880 KB
testcase_01 AC 28 ms
10,880 KB
testcase_02 AC 28 ms
10,880 KB
testcase_03 AC 48 ms
11,520 KB
testcase_04 AC 46 ms
11,392 KB
testcase_05 AC 45 ms
11,520 KB
testcase_06 AC 40 ms
11,264 KB
testcase_07 AC 40 ms
11,392 KB
testcase_08 AC 40 ms
11,392 KB
testcase_09 AC 48 ms
11,520 KB
testcase_10 AC 29 ms
11,008 KB
testcase_11 AC 29 ms
11,008 KB
testcase_12 AC 29 ms
11,008 KB
testcase_13 AC 29 ms
10,880 KB
testcase_14 AC 28 ms
10,880 KB
testcase_15 AC 28 ms
10,880 KB
testcase_16 AC 852 ms
41,400 KB
testcase_17 AC 863 ms
21,888 KB
testcase_18 AC 855 ms
55,660 KB
testcase_19 AC 980 ms
40,624 KB
testcase_20 AC 949 ms
27,028 KB
testcase_21 AC 979 ms
27,444 KB
testcase_22 AC 969 ms
42,672 KB
testcase_23 AC 951 ms
42,920 KB
testcase_24 AC 898 ms
43,052 KB
testcase_25 AC 937 ms
41,408 KB
testcase_26 AC 938 ms
41,396 KB
testcase_27 AC 954 ms
41,408 KB
testcase_28 AC 945 ms
41,400 KB
testcase_29 AC 968 ms
41,392 KB
testcase_30 AC 970 ms
41,772 KB
testcase_31 AC 939 ms
41,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


class UnionFind:
    n = 1
    parent_or_size = [-1 for i in range(n)]

    def __init__(self, n0):
        self.n = n0
        self.parent_or_size = [-1 for _ in range(n0)]

    def merge(self, a, b):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        assert 0 <= b < self.n, "0<=b<n,b={0},n={1}".format(b, self.n)
        x = self.leader(a)
        y = self.leader(b)
        if x == y:
            return x
        if -self.parent_or_size[x] < -self.parent_or_size[y]:
            x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        assert 0 <= b < self.n, "0<=b<n,b={0},n={1}".format(b, self.n)
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        if self.parent_or_size[a] < 0:
            return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        assert 0 <= a < self.n, "0<=a<n,a={0},n={1}".format(a, self.n)
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        leader_buf = [0 for _ in range(self.n)]
        group_size = [0 for _ in range(self.n)]
        for i in range(self.n):
            leader_buf[i] = self.leader(i)
            group_size[leader_buf[i]] += 1
        result = [[] for _ in range(self.n)]
        for i in range(self.n):
            result[leader_buf[i]].append(i)
        result2 = []
        for i in range(self.n):
            if len(result[i]) > 0:
                result2.append(result[i])
        return result2


def main():
    n, m = map(int, input().split())

    graph = defaultdict(list)
    for _ in range(n):
        b, c = map(int, input().split())
        b -= 1
        c -= 1
        graph[b].append(c)

    uf = UnionFind(n)
    for v in graph.values():
        for x, y in zip(v, v[1:]):
            uf.merge(x, y)

    cnt = [-1] * n
    for v in graph.values():
        cnt[uf.leader(v[0])] += 1

    ans = sum([x for x in cnt if x >= 1])
    print(ans)


if __name__ == "__main__":
    main()
0