結果
問題 | No.1390 Get together |
ユーザー |
|
提出日時 | 2022-04-15 06:08:49 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 464 ms / 2,000 ms |
コード長 | 2,019 bytes |
コンパイル時間 | 452 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 97,044 KB |
最終ジャッジ日時 | 2024-12-24 15:08:05 |
合計ジャッジ時間 | 10,884 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
# import pypyjit# pypyjit.set_param('max_unroll_recursion=-1')import sysfrom itertools import combinations, permutations, product, accumulate, groupbyfrom collections import defaultdict, deque, Counterfrom functools import reducefrom operator import add, mulimport heapqimport bisectimport mathimport copy# sys.setrecursionlimit(10 ** 7)input = lambda: sys.stdin.readline().rstrip()INF = float("inf")MOD = 10 ** 9 + 7from collections import defaultdictclass UnionFind():def __init__(self, n):self.n = nself.parents = [-1] * ndef find(self, x):if self.parents[x] < 0:return xelse: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:returnif self.parents[x] > self.parents[y]:x, y = y, xself.parents[x] += self.parents[y]self.parents[y] = xdef 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_membersdef __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)]for i in range(N):b, c = map(int, input().split())L[c].append(b)uf = UnionFind(M + 1)cnt = 0for l in L:for c in l:if not uf.same(l[0], c):uf.union(l[0], c)cnt += 1print(cnt)