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: def DFS(s):
    flag[s] = 1
    F[group] += s
    for i in range(n):
        if G[s][i] == 1:
            if flag[i] == 0:
                DFS(i)
 

n, m = map(int, input().split())
G = [[0 for i in range(n)] for j in range(n)]
for i in range(m):
    s, t = map(int, input().split())
    G[s][t] = 1
    G[t][s] = 1

F = []
flag = [0 for i in range(n)]
group = 0
for i in range(n):
    if flag[i] == 0:
        F.append([])
        DFS(i)
        group += 1

q = int(input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, input().split())
    for friends in F:
        if s in friends and t in friends:
            ans[i] = "yes"

for i in range(q):
    print(ans[i])

Python: class QuickUnion:
    def __init__(self, n):
        self.conn = [i for i in range(n)]
        self.size = [1 for _ in range(n)]

    def union(self, a, b):
        ra = self._root(a)
        rb = self._root(b)

        if ra == rb:
            return

        if self.size[ra] > self.size[rb]:
            self.conn[rb] = ra
            self.size[ra] += self.size[rb]
        else:
            self.conn[ra] = rb
            self.size[rb] += self.size[ra]

    def connected(self, a, b):
        ra = self._root(a)
        rb = self._root(b)

        return ra == rb

    def _root(self, a):
        p = self.conn[a]
        while p != a:
            pp = self.conn[p]
            self.conn[a] = pp
            a, p = p, pp

        return a


def run():
    n, m = [int(i) for i in input().split()]
    u = QuickUnion(n)

    for _ in range(m):
        x, y = [int(i) for i in input().split()]
        u.union(x, y)

    q = int(input())

    for _ in range(q):
        x, y = [int(i) for i in input().split()]
        if u.connected(x, y):
            print('yes')
        else:
            print('no')


if __name__ == '__main__':
    run()

Python: n, m = map(int, input().split())

friend = [ [] for _ in range(n) ]

for _ in range(m):
    s, t = map(int, input().split())
    friend[s].append(t)
    friend[t].append(s)

flag = [0] * n
group = 0

# def dfs(u):
#    for v in friend[u]:
#        if flag[v] == 0:
#            flag[v] = group
#            dfs(v)

for i in range(n):
    if flag[i] == 0:
        group += 1
        flag[i] = group
        # dfs(i)
        
        stack = [i]
        
        while stack:
            u = stack.pop()
            for v in friend[u]:
                if flag[v] == 0:
                    flag[v] = group
                    stack.append(v)

q = int(input())

for _ in range(q):
    s, t = map(int, input().split())
    if flag[s] == flag[t]:
        print('yes')
    else:
        print('no')

Python: import sys
from collections import deque
input = sys.stdin.readline

V, E = map(int, input().split())
G = {i: deque([]) for i in range(V)}
for _ in range(E):
    v1, v2 = map(int, input().split())
    G[v1].append(v2)
    G[v2].append(v1)


seen = [0]*(V)
stack = []
color = {v: -1 for v in range(V)}

def dfs(j, col):
    global time
    if seen[j]:
        return
    seen[j] = 1
    stack.append(j)
    color[j] = col
    while stack:
        s = stack[-1]
        if not G[s]:
            stack.pop()
            continue
        g_NO  = G[s].popleft()
        if seen[g_NO]:
            continue
        color[g_NO] = col
        seen[g_NO] = 1
        stack.append(g_NO)
    
c = 0
for start in range(V):
    if seen[start]:
        continue
    dfs(start, c)
    c += 1


q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    if color[s] != -1 and color[t] != -1 and color[s] == color[t]:
        print("yes")
    else:
        print("no")

Python: n,m = map(int,input().split())
Rel = [tuple(map(int,input().split())) for _ in range(m)]
q = int(input())
Que = [tuple(map(int,input().split())) for _ in range(q)]

class UF:
    def __init__(self):
        self.roots = [i for i in range(n)]
        self.rank = [1 for _ in range(n)]

    def same(self,a,b):
        if self.find(a) == self.find(b):
            return True
        else:
            return False

    def find(self,a):
        if self.roots[a] == a:
            return a
        else:
            self.roots[a] = self.find(self.roots[a])
            return self.roots[a]

    def union(self,a,b):
        a,b = self.find(a),self.find(b)
        if a != b:
            if self.rank[a] >= self.rank[b]:
                self.roots[b] = a
                if self.rank[a] == self.rank[b]:
                    self.rank[b] += 1
            else:
                self.roots[a] = b

uf = UF()

for i in range(m):
    uf.union(Rel[i][0],Rel[i][1])

for i in range(q):
    if uf.same(Que[i][0],Que[i][1]):
        print("yes")
    else:
        print("no")

Python: #!/usr/bin/python3
n, m = map(int, input().split())
ALL = [0] * n
Graph = []
for i in range(m):
    s, t = map(int, input().split())
    if ALL[s] == 0 and ALL[t] == 0:
        Graph.append(set([s, t]))
    elif ALL[s] == 0:
        for g in Graph:
            if t in g:
                g.add(s)
                break
    elif ALL[t] == 0:
        for g in Graph:
            if s in g:
                g.add(t)
                break
    else:
        sg = None
        tg = None
        for g in Graph:
            if s in g:
                sg = g
            if t in g:
                tg = g

        if sg is not tg:
            sg.update(tg)
            Graph.remove(tg)

    ALL[s] = 1
    ALL[t] = 1
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    for g in Graph:
        if s in g and t in g:
            print('yes')
            break
    else:
        print('no')