Lập Trình Viên Số Khổ
Senior Member
Python:
class Solution:
def equationsPossible(self, equations: List[str]) -> bool:
class Ufds:
parent_node = {}
def make_set(self, u):
for i in u:
self.parent_node[i] = i
def op_find(self, k):
if self.parent_node[k] == k:
return k
return self.op_find(self.parent_node[k])
def op_union(self, a, b):
x = self.op_find(a)
y = self.op_find(b)
self.parent_node[y] = x
chars = set()
for e in equations:
chars.add(e[0])
chars.add(e[3])
uf = Ufds()
uf.make_set(chars)
for e in equations:
if "!" not in e:
uf.op_union(e[0], e[3])
for e in equations:
if "!" in e \
and (uf.op_find(e[0]) == uf.op_find(e[3]) \
or uf.op_find(e[3]) == uf.op_find(e[0])):
return False
return True

union find à?
thôi bỏ, đợi solution

