forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
number-of-good-paths.py
46 lines (39 loc) · 1.36 KB
/
number-of-good-paths.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
# Time: O(nlogn)
# Space: O(n)
import collections
class UnionFind(object): # Time: O(n * alpha(n)), Space: O(n)
def __init__(self, vals):
self.set = range(len(vals))
self.rank = [0]*len(vals)
self.cnt = [collections.Counter({v:1}) for v in vals] # added
def find_set(self, x):
stk = []
while self.set[x] != x: # path compression
stk.append(x)
x = self.set[x]
while stk:
self.set[stk.pop()] = x
return x
def union_set(self, x, y, v): # modified
x, y = self.find_set(x), self.find_set(y)
if x == y:
return 0 # modified
if self.rank[x] > self.rank[y]: # union by rank
x, y = y, x
self.set[x] = self.set[y]
if self.rank[x] == self.rank[y]:
self.rank[y] += 1
cx, cy = self.cnt[x][v], self.cnt[y][v] # added
self.cnt[y] = collections.Counter({v:cx+cy}) # added
return cx*cy # modified
# tree, sort, union find
class Solution(object):
def numberOfGoodPaths(self, vals, edges):
"""
:type vals: List[int]
:type edges: List[List[int]]
:rtype: int
"""
edges.sort(key=lambda x: max(vals[x[0]], vals[x[1]]))
uf = UnionFind(vals)
return len(vals)+sum(uf.union_set(i, j, max(vals[i], vals[j])) for i, j in edges)