Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

Python: import sys


if __name__ == "__main__":
    n, m = map(int, sys.stdin.readline().split())
    setlist = []
    inp = sys.stdin.readlines()
    for i in range(m):
        inp_i = list(map(int, inp[i].split()))
        a = -1
        b = -1
        for j in range(len(setlist)):
            if inp_i[0] in setlist[j]:
                a = j
            if inp_i[1] in setlist[j]:
                b = j
        if a == -1 and b == -1:
            setlist.append(set([inp_i[0], inp_i[1]]))
        elif a == -1:
            setlist[b].add(inp_i[0])
        elif b == -1:
            setlist[a].add(inp_i[1])
        elif a < b:
            c = setlist.pop(a)
            d = setlist.pop(b-1)
            e = c.union(d)
            setlist.append(e)
        elif a > b:
            c = setlist.pop(b)
            d = setlist.pop(a-1)
            e = c.union(d)
            setlist.append(e)
    for i in range(int(inp[m])):
        inp_i = list(map(int, inp[i+m+1].split()))
        if len(setlist) == 0:
            print("no")
        for j in range(len(setlist)):
            if inp_i[0] in setlist[j]:
                if inp_i[1] in setlist[j]:
                    print("yes")
                    break
                else:
                    print("no")
                    break
            if j == len(setlist)-1:
                print("no")

Python: def tan(G,st,fi,flag):
    for i in range(len(G)):
        if G[st][i] == 1:
            print(st,fi,i)
            if i == fi:
                flag == 1
            tan(G,i,fi,flag)
    
n,m = list(map(int, input().split()))
G = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    S = list(map(int, input().split()))
    G[S[0]][S[1]] = 1

q = int(input())
ans = []
for i in range(q):
    Q = list(map(int, input().split()))
    st = Q[0]
    fi = Q[1]
    flag = 0
    tan(G,Q[0],Q[1],flag)
    if flag==1:
        ans.append("yes")
    else:
        ans.append("no")
        
for i in range(q):
    print(ans[i])

Python: from collections import deque
n,m = [int(i) for i in input().split()]
F = [{} for i in range(n)]
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
DP = [0 for i in range(n)]
c = 1
d = deque()
for i in range(n):
    if DP[i]:
        continue
    else:
        d.append(i)
    while(len(d)>0):
        tmp = d.pop()
        DP[tmp] = c
        for i in F[tmp]:
            if not DP[i]:
                d.append(i)
    c+=1

for i in range(q):
    s,t = [int(i) for i in input().split()]
    if DP[s] == DP[t]:
        print("yes")
    else:
        print("no")

Python: # -*- coding: utf-8 -*-
import sys


def assignColor():
    for i in range(n):
        if C[i] == -1:
            #BFS(i)
            DFS(i)

def BFS(start):
    global C
    Q = [start]
    C[start] = start

    while len(Q) != 0:
        u = Q.pop(0)
        v = [i for i in G[u] if C[i] == -1] # v := ??£??\???????????????????????¢?´¢??????????????????
        
        for i in v:
            Q.append(i)
            C[i] = C[start]

#@profile
def DFS(start):
    S = [start]
    C[start] = start
    while len(S) != 0:
        u = S.pop(0)
        for i in G[u]:
            if C[i] == -1:
                C[i] = start
                S.append(i)

if __name__ == "__main__":
    _input = sys.stdin.readlines()
    n, m = list(map(int, _input[0].split()))
    G = [[] for i in range(n)]
    C = [-1]*n
    
    for i in range(m):
        s, t = list(map(int, _input[i+1].split()))
        G[s].append(t)
        G[t].append(s)
    assignColor()
    q = int(_input[m+1])
    for i in range(q):
        s, t = list(map(int, _input[m+i+2].split()))
        if C[s] == C[t]:
            print("yes")
        else:
            print("no")

Python: import sys


if __name__ == "__main__":
    n, m = map(int, sys.stdin.readline().split())
    setlist = []
    inp = sys.stdin.readlines()
    for i in range(m):
        inp_i = list(map(int, inp[i].split()))
        for j in range(len(setlist)):
            if inp_i[0] in setlist[j] or inp_i[1] in setlist[j]:
                setlist[j].add(inp_i[0])
                setlist[j].add(inp_i[1])
                break
            if j == len(setlist)-1:
                setlist.append(set([inp_i[0], inp_i[1]]))
        if i == 0:
            setlist.append(set([inp_i[0], inp_i[1]]))
    for i in range(int(inp[m])):
        inp_i = list(map(int, inp[i+m+1].split()))
        if len(setlist) == 0:
            print("no")
        for j in range(len(setlist)):
            if inp_i[0] in setlist[j]:
                if inp_i[1] in setlist[j]:
                    print("yes")
                    break
                else:
                    print("no")
                    break
            if j == len(setlist)-1:
                print("no")

Python: from collections import deque
from enum import Enum, auto

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

n, m = list(map(int, input().split(' ')))
adj_list = [[] for i in range(n)]
for i in range(m):
    u, v = list(map(int, input().split(' ')))
    adj_list[u].append(v)
    adj_list[v].append(u)

def adj(s, e):
    queue = deque([])
    colors = [Color.WHITE for i in range(n)]

    queue.append(s)
    colors[s] = Color.GRAY

    while len(queue) > 0:
        u = queue.popleft()
        for v in adj_list[u]:
            if colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                queue.append(v)
                if v == e: return True
        colors[u] = Color.BLACK
    return False

q = int(input())
for i in range(q):
    s, e = list(map(int, input().split(' ')))
    if adj(s, e):
        print('yes')
    else:
        print('no')