Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

Python: # -*- coding: utf-8 -*-
from collections import deque


if __name__ == '__main__':

    n, m = [int(s) for s in input().split(" ")]
    M = [[] for j in range(n)]
    for _ in range(m):
        u, v = [int(s) for s in input().split(" ")]
        M[u].append(v)
        M[v].append(u)
        
    color = [0] * n
    print(M)
    def dfs(u, c):  # depth-first search
        color[u] = c
        for v in range(n):
            if v in M[u] and color[v] == 0:
                dfs(v, c)
        color[u] = c
    
    c = 1
    for u in range(n):
        if color[u] == 0:
            dfs(u, c)
            c += 1
    
    l = int(input())
    for _ in range(l):
        p, q = [int(s) for s in input().split(" ")]
        if color[p] == color[q]:
            print("yes")
        else:
            print("no")

Python: from collections import deque

n, m = map(int, input().split())
g = [set() for _ in range(n)]
gids = [0] * n

while m:
    s, t = map(int, input().split())
    g[s].add(t)
    g[t].add(s)
    m -= 1


def search(s, gid):
    global g, gids
    queue = deque()
    queue.append(s)
    visited = [0] * n
    visited[s] = 1
    gids[s] = gid

    while queue:
        i = queue.popleft()
        for f in g[i]:
            if not visited[f]:
                visited[f] = 1
                gids[f] = gid
                queue.append(f)


gid = 1
for i in range(n):
    if gids[i]:
        continue
    search(i, gid)
    gid += 1

print(gids)

q = int(input())
while q:
    s, t = map(int, input().split())
    print('yes' if gids[s] == gids[t] else 'no')
    q -= 1

Python: import sys
 
def make_connected_group(G):
    C = [None]*len(G)
    group = 0
    for i,p in enumerate(C):
        if p != None: continue
        friends = [i]
        while len(friends) > 0:
            f = friends.pop()
            if C[f] == None:
                C[f] = group
                friends += G[f]
        group += 1
    return C
 
 
if __name__=='__main__':
    n,m = list(map(int,sys.stdin.readline().split()))
    G = [[] for _ in range(n)]
    for j,line in enumerate(sys.stdin):
        if not j < m:
            q = int(line)
            break
        s,t = list(map(int,line.split()))
        G[s].append(t)
        G[t].append(s)
    C = make_connected_group(G)
    for j,line in enumerate(sys.stdin):
        if not j < q: break
        s,t = list(map(int,line.split()))
        print('yes' if C[s] == C[t] else 'no')

Python: def dfs(G, C, id, color):
    S = []
    S.append(id)
    C[id] = color

    while S:
        u = S[-1]  # ?????¨?¨????????????????????????°??????
        S = S[:-1]  # pop
        for i in range(len(G[u])):
            v = G[u][i]
            if C[v] == -1:
                C[v] = color
                S.append(v)


if __name__ == '__main__':
    # ??????????????\???
    num_of_users, num_of_links = [int(x) for x in input().split(' ')]
    links = []
    for _ in range(num_of_links):
        links.append(list(map(int, input().split(' '))))
    num_of_queries = int(input())
    queries = []
    for _ in range(num_of_queries):
        queries.append(list(map(int, input().split(' '))))

    # ???????????????
    G = [[] for _ in range(11)]
    C = [-1] * 11
    for f, t in links:
        G[f].append(t)
        G[t].append(f)

    color = 1
    for id in range(num_of_users):
        if C[id] == -1:
            dfs(G, C, id, color)
        color += 1

    # ???????????????
    for x, y in queries:
        if C[x] == C[y]:
            print('yes')
        else:
            print('no')

Python: import sys


def dfs(u, col):
    global time
    color[u] = "GRAY"
    for v in range(n):
        if v in M[u] and color[v] == "WHITE":
            dfs(v, col)
    color[u] = col


if __name__ == "__main__":
    n, m = map(int, sys.stdin.readline().split())
    color = ["WHITE"] * n
    M = [[] for i in range(n)]
    inp = sys.stdin.readlines()
    for i in range(m):
        inp_i = list(map(int, inp[i].split()))
        M[inp_i[0]].append(inp_i[1])
        M[inp_i[1]].append(inp_i[0])
    for i in range(n):
        if color[i] == "WHITE":
            dfs(i, i)
    for i in range(int(inp[m])):
        inp_i = list(map(int, inp[i+m+1].split()))
        if color[inp_i[0]] == color[inp_i[1]]:
            print("yes")
        else:
            print("no")

Python: from collections import deque


def bfs(root):
    global time
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in adjDict[i]:
            if color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black'
        label[i] = time


firstline = list(map(int, input().split()))

n = firstline[0]
adjDict = {i: [] for i in range(n)}
color = ['white'] * n
distance = [-1] * n
label = [-1] * n

m = firstline[1]
for _ in range(m):
    adj = list(map(int, input().split()))
    adjDict[adj[0]].append(adj[1])
    adjDict[adj[1]].append(adj[0])

time = 0
for i in range(n):
    if color[i] == 'white':
        bfs(i)
        time += 1

q = int(input())
for _ in range(q):
    question = list(map(int, input().split()))
    if label[question[0]] == label[question[1]]:
        print('yes')
    else:
        print('no')