-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1319.py
More file actions
166 lines (130 loc) · 4.99 KB
/
1319.py
File metadata and controls
166 lines (130 loc) · 4.99 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
__________________________________________________________________________________________________
sample 464 ms submission
class Solution:
def makeConnected(self, n: int, con: List[List[int]]) -> int:
p = [i for i in range(n)]
cnt = n
spare = 0
def find(x):
while x != p[x]:
p[x] = p[p[x]]
x = p[x]
return x
for x,y in con:
p1 = find(x)
p2 = find(y)
if p1 != p2:
p[p1] = p2
cnt -= 1
else:
spare += 1
# print(x, y, p1, p2, cnt, spare)
if cnt == 1:
return 0
elif spare >= cnt-1:
return cnt-1
else:
return -1
__________________________________________________________________________________________________
sample 480 ms submission
class DisjointSet:
def __init__(self, idx):
self.head = idx
self.tail = idx
self.length = 1
class ListNode:
def __init__(self, idx):
self.val = idx
self.next = None
self.set = None
class Solution:
# Method 1: disjoint set linked-list implementation
def makeConnected(self, n, connections):
nodeList = [] # node list should be in the same order of occurrence with node indices
Unions = [] # a list of all unions
def make_set(x):
# # tell if x already in some other sets
# if x in visited:
# return
Sx = DisjointSet(x)
Unions.append(Sx)
return Sx
def find_set(x):
return nodeList[x].set.head
def union(x, y):
if find_set(x) == find_set(y): return
Sx = nodeList[x].set
Sy = nodeList[y].set
# always keep the longer linked-list and append the shorter linked-list
if Sx.length < Sy.length: Sx, Sy = Sy, Sx
# append Sy at the end of Sx
nodeList[Sx.tail].next = nodeList[Sy.head]
Sx.tail = Sy.tail
# modify the set pointer of elements in Sy
pointer = nodeList[Sy.head]
while pointer:
pointer.set = Sx
pointer = pointer.next
Sx.length += Sy.length
Unions.remove(Sy)
del Sy
for i in range(n):
node = ListNode(i)
nodeList.append(node)
node.set = make_set(i)
# init
redunConnNum = 0
for v1, v2 in connections:
# print([v1, v2])
if find_set(v1) == find_set(v2):
redunConnNum += 1
else:
union(v1, v2)
print(redunConnNum, len(Unions) - 1)
return -1 if redunConnNum < len(Unions) - 1 else len(Unions) - 1
# Method 2: disjoint set rooted-tree implementation
# time = 5%, space = 100%
def makeConnected(self, n, connections):
def union(x, y):
if DisjointSetRepres[x] == DisjointSetRepres[y]: return
tempRepres = DisjointSetRepres[y]
for vertex in DisjointSets[tempRepres]:
DisjointSetRepres[vertex] = DisjointSetRepres[x]
DisjointSets[DisjointSetRepres[x]] += DisjointSets[tempRepres]
DisjointSets.pop(tempRepres, None)
# init var
DisjointSetRepres = dict()
DisjointSets = dict()
redunConnNum = 0
# init (make_set function)
for i in range(n):
DisjointSetRepres[i] = i
DisjointSets[i] = [i]
for v1, v2 in connections:
# (find_set function)
if DisjointSetRepres[v1] == DisjointSetRepres[v2]:
redunConnNum += 1
else:
union(v1, v2)
return -1 if redunConnNum < len(DisjointSets) - 1 else len(DisjointSets) - 1
# disjoint set simplified version of rooted-tree implementation
# time = 99%, space = 100%
def makeConnected(self, n, connections):
r = [i for i in range(n)] # let r denote the representative of the set which contains this element
unionNum = n
redunConnNum = 0
def find(x):
while x != r[x]:
r[x] = r[r[x]] # path compression operation is done here
x = r[x]
return x
for x, y in connections:
r1 = find(x) # find representative of x
r2 = find(y) # find representative of y
if r1 != r2:
r[r1] = r2 # union: set representative of r1 as r2
unionNum -= 1 # every union operation would reduce union number by 1
else:
redunConnNum += 1
return -1 if redunConnNum < unionNum - 1 else unionNum - 1
__________________________________________________________________________________________________