-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerging_community.py
61 lines (51 loc) · 1.89 KB
/
merging_community.py
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# https://www.hackerrank.com/challenges/merging-communities/problem
# its all about disjoint set.
class DisjointSet(object):
def __init__(self):
self.leader = {} # maps a member to the group's leader
self.group = {} # maps a group leader to the group (which is a set)
def add(self, a, b):
leadera = self.leader.get(a)
leaderb = self.leader.get(b)
if leadera is not None:
if leaderb is not None:
if leadera == leaderb: return # nothing to do
groupa = self.group[leadera]
groupb = self.group[leaderb]
if len(groupa) < len(groupb):
a, leadera, groupa, b, leaderb, groupb = b, leaderb, groupb, a, leadera, groupa
groupa |= groupb
del self.group[leaderb]
for k in groupb:
self.leader[k] = leadera
else:
self.group[leadera].add(b)
self.leader[b] = leadera
else:
if leaderb is not None:
self.group[leaderb].add(a)
self.leader[a] = leaderb
else:
self.leader[a] = self.leader[b] = a
self.group[a] = set([a, b])
ds = DisjointSet()
N,Q = map(int,input().split())
for _ in range(Q):
s = input().split()
if s[0] is 'Q':
flag = False
if len(ds.group.items()) == 0:
ds.add(s[1],s[1])
print(len(ds.group[s[1]]))
flag = True
else:
key = ds.leader.get(s[1],False)
if key:
val = ds.group[key]
print(len(val))
flag = True
if not flag:
ds.add(s[1],s[1])
print(len(ds.group[s[1]]))
else:
ds.add(s[1],s[2])