今日专题:并查集
107. 寻找存在的路径
思路:采用并查集的结构,在把图录进去后,判断起点和终点的根是否一致
#其实bfs也可以做吧fa={}
def find(u):if fa.get(u)== None or fa[u]==u:return uelse:fa[u]=find(fa[u])return fa[u]def join(u,v):u=find(u)v=find(v)if u==v : returnfa[u]=vdef main():n,m=map(int,input().split())for i in range(m):x,y=map(int,input().split())if fa.get(x)== None:fa[x]=xif fa.get(y)==None:fa[y]=yjoin(x,y)x,y=map(int,input().split())res= 1 if find(x)==find(y) else 0print(res)if __name__=="__main__":main()