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: from collections import deque

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

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


def find(s, t):
    global g
    queue = deque()
    queue.append((0, s))
    queue.append((1, t))
    visited = (set(), set())
    visited[0].add(s)
    visited[1].add(t)

    def remember(visited, new):
        g[new] |= visited
        for v in visited:
            g[v].add(new)
        visited.add(new)

    while queue:
        st, i = queue.popleft()
        for f in g[i]:
            if f in visited[not st]:
                return True
            if f not in visited[st]:
                remember(visited[st], f)
                queue.append((st, f))
    return False


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

Python: def DFS(s):
    group[s] = j
    for i in range(n):
        if i in G[s] and group[i] == 0:
            DFS(i)

n, m = map(int, input().split())
G = [[] for j in range(n)]

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

group = [0 for i in range(n)]
j = 1
for i in range(n):
    if group[i] == 0:
        DFS(i)
        j += 1

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

Python: from collections import deque
 
n, m = map(int, input().split())

adj = [[] for _ in range(n)] # adjacent list

for _ in range(m):
    u, v = map(int, input().split())
    adj[u].append(v)
    adj[v].append(u)

c = ['w']*n # color of each vertex
con = [0]*n
cnt = 0


def BFS_visit(s):
    global c
    global cnt
    global con
    
    c[s] = 'g'
    con[s] = cnt
    
    Q = deque()
    Q.append(s)
    while Q:
        u = Q.popleft()
        for v in adj[u]:
            if c[v] == 'w':
                c[v] = 'g'
                con[v] = cnt
                Q.append(v)
        c[u] = 'b'
        
for u in range(n):
    if c[u] == 'w':
        cnt += 1
        BFS_visit(u)
        
        
q = int(input())
for _ in range(q):
    u, v = map(int, input().split())
    ans = "yes" if (con[u] == con[v]) else "no"
    print(ans)

Python: def main():
    n, m = map(int, input().split())
    adj = [[] for i in range(n)]
    for i in range(m):
        s, t = map(int, input().split())
        adj[s].append(t)
        adj[t].append(s)

    q = int(input())
    component_ids = [-1 for i in range(n)]
    id = 0
    for i in range(n):
        if component_ids[i] != -1:
            continue
        bfs(i, id, adj, component_ids)
        id += 1

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


def bfs(node, id, adj, component_ids):
    que = [node]
    while que:
        u = que.pop(0)
        component_ids[u] = id
        for v in adj[u]:
            if component_ids[v] == -1:
                que.append(v)

main()

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 not DP[i]:
        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: n, m = map(int, input().split())
G = [[] for j in range(n)]
for i in range(m):
    s, t = map(int, input().split())
    G[s].append(t)
    G[t].append(s)

group = []
c = 0
for i in range(n):
    group.append([])
    if not i in group:
        que = [i]
        group[i] = c
        while len(que) > 0:
            u = que[0]
            for j in G[u]:
                if not j in group:
                    group[j] = que.append(j)
            del que[0]
        c += 1

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

Python: from sys import stdin
input = stdin.readline

from collections import deque, defaultdict
from copy import deepcopy

MAX = 100000
NIL = -1

# Gはdictで管理する、valueはdeque
G = defaultdict(lambda: deque())
color = [NIL]*MAX

def dfs(r, c):
    S = deque()
    S.append(r)
    color[r] = c
    while S:
        u = S.popleft()
        for v in G[u]:  # Gのvalueはdequeなので、それをvに一つずつ入れていく
            if color[v] == NIL:
                color[v] = c
                S.append(v)

def assign_color():
    id = 1
    for u in range(n):
        if color[u] == NIL:
            dfs(u, id)
            id += 1

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

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

assign_color()

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