結果

問題 No.1390 Get together
ユーザー itsy68itsy68
提出日時 2022-10-18 03:56:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 465 ms / 2,000 ms
コード長 2,061 bytes
コンパイル時間 329 ms
コンパイル使用メモリ 87,184 KB
実行使用メモリ 100,640 KB
最終ジャッジ日時 2023-09-11 00:16:22
合計ジャッジ時間 11,918 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
76,644 KB
testcase_01 AC 120 ms
76,748 KB
testcase_02 AC 120 ms
77,056 KB
testcase_03 AC 154 ms
80,704 KB
testcase_04 AC 150 ms
80,604 KB
testcase_05 AC 160 ms
80,836 KB
testcase_06 AC 149 ms
80,712 KB
testcase_07 AC 149 ms
80,624 KB
testcase_08 AC 151 ms
80,540 KB
testcase_09 AC 156 ms
80,952 KB
testcase_10 AC 122 ms
77,052 KB
testcase_11 AC 121 ms
76,752 KB
testcase_12 AC 122 ms
76,912 KB
testcase_13 AC 121 ms
76,868 KB
testcase_14 AC 121 ms
76,924 KB
testcase_15 AC 123 ms
76,676 KB
testcase_16 AC 221 ms
98,712 KB
testcase_17 AC 336 ms
98,412 KB
testcase_18 AC 219 ms
98,628 KB
testcase_19 AC 422 ms
100,056 KB
testcase_20 AC 410 ms
99,624 KB
testcase_21 AC 465 ms
100,632 KB
testcase_22 AC 331 ms
98,496 KB
testcase_23 AC 335 ms
98,340 KB
testcase_24 AC 342 ms
98,392 KB
testcase_25 AC 440 ms
100,640 KB
testcase_26 AC 412 ms
99,776 KB
testcase_27 AC 418 ms
100,160 KB
testcase_28 AC 421 ms
99,244 KB
testcase_29 AC 414 ms
99,688 KB
testcase_30 AC 432 ms
100,256 KB
testcase_31 AC 408 ms
99,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import combinations, permutations, product, accumulate, groupby
from collections import defaultdict, deque, Counter
from functools import reduce
import heapq
import bisect
import math
import copy

sys.setrecursionlimit(10 ** 9)
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
INF = float("inf")
MOD = 10 ** 9 + 7
# DFS
# import pypyjit
# pypyjit.set_param('max_unroll_recursion=-1')


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())
# 0-indexed

N, M = map(int, input().split())

uf = UnionFind(M)
L = [[] for i in range(N)]
for i in range(N):
    b, c = mi()
    b-=1
    c-=1
    L[c].append(b)

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