N, Q = map(int, input().split()) P = list(map(int, input().split())) for i in range(N): if P[i] != -1: P[i] -= 1 C = [-1]*N for i, p in enumerate(P): if p != -1: C[p] = i R = [-1]*N for i in range(N): if P[i] != -1: continue R[i] = i while (i := C[i]) != -1: R[i] = R[P[i]] for _ in range(Q): a, b = map(int, input().split()) print("Yes" if R[a-1] == R[b-1] else "No")