-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathsolution.py
More file actions
34 lines (28 loc) · 1014 Bytes
/
solution.py
File metadata and controls
34 lines (28 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# This challenge use a disjoint set with set size tracking.
# In addition to the basic disjoint set operations, we keep
# track of the total elements in a set with (#1), and add a
# method (#2) to get the set size.
class DisjointSet:
def __init__(self, N):
self.parent = [i for i in range(N)]
self.total = [1] * N #1
def union(self, a, b):
a_parent = self.find(a)
b_parent = self.find(b)
if a_parent != b_parent:
self.parent[b_parent] = a_parent
self.total[a_parent] += self.total[b_parent] #1
def find(self, a):
if self.parent[a] != a:
self.parent[a] = self.find(self.parent[a])
return self.parent[a]
def get_total(self, a): #2
return self.total[self.find(a)]
N, Q = map(int, input().split())
ds = DisjointSet(N)
for i in range(Q):
op, *x = input().split()
if op == 'M':
ds.union(int(x[0]) - 1, int(x[1]) - 1)
else:
print(ds.get_total(int(x[0]) - 1))