# class Solution:
# def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
# if [source,destination] in edges or [destination,source] in edges: return True
# if source==destination: return True
# s0=[]
# hasnew={}
# visit=set()
# for x,y in edges:
# hasnew={x,y}-visit
# if len(hasnew)==2:
# s0.append(hasnew)
# visit|={x,y}
# elif len(hasnew)==0:
# a,b=-1,-1
# for i,s in enumerate(s0):
# if x in s:
# a=i
# if b!=-1:
# if a!=b:
# s0[b]|=s
# if source in s0[b] and destination in s0[b]:
# return True
# s0.pop(a)
# break
# if y in s:
# b=i
# if a!=-1:
# if a!=b:
# s0[a]|=s
# if source in s0[a] and destination in s0[a]:
# return True
# s0.pop(b)
# break
# else:
# visit|=hasnew
# tx=list({x,y}-hasnew)[0]
# for i,s in enumerate(s0):
# if tx in s:
# s0[i]|=hasnew
# if source in s0[i] and destination in s0[i]:
# return True
# break
# return False
class Solution:
def validPath(self, n: int, edges: List[List[int]], source: int, destination: int) -> bool:
reachable =set([source])
nE = len(edges)
while 1:
n = len(reachable)
for ii in range(nE):
if edges[ii][0] in reachable:
reachable.add(edges[ii][1])
elif edges[ii][1] in reachable:
reachable.add(edges[ii][0])
if destination in reachable:
return True
if len(reachable) == n:
return False