模板:
# init p = [i for i in range(N + 1)] def union(p, i, j): p1 = parent(p, i) p2 = parent(p, j) p[p1] = p2 def parent(p, i): root = i while p[root] != root: root = p[root] while p[i] != i: x = i; i = p[i]; p[x] = root return root
# init def union(p, i, j): p1 = parent(p, i) p2 = parent(p, j) p[p1] = p2 def parent(p, i): root = i while p[root] != root: root = p[root] while p[i] != i: x = i; i = p[i]; p[x] = root return root # 创建并查集 N, M = map(int, input().split()) # p = [i for i in range(N + 1)] while (M): a, b = map(int, input().split()) union(p, a, b) M -= 1 Q = int(input()) while (Q): c, d = map(int, input().split()) if (parent(p, c) == parent(p, d)): print("Yes") else: print("No") Q -= 1