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: import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

class UnionFind():
    
    def __init__(self,n):
        self.n = n
        self.parents = [-1]*n #各要素の親要素の番号を格納するリスト
    
    #要素xが属するグループの根を返す
    def find(self,x):
        if self.parents[x]<0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    
    #要素xと要素yが属するグループを併合する   
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        
        if self.parents[x]>self.parents[y]:
            x,y = y,x
        
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    #要素xが属するグループのサイズを返す
    def size(self,x):
        return -self.parents[self.find(x)]
    
    #要素x,yが同じグループかどうかを返す
    def same(self,x,y):
        return self.find(x) == self.find(y)
    
    #要素xと同じグループのメンバーを返す
    def members(self,x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    
    #すべての根の要素を返す
    def roots(self):
        return [i for i,x in enumerate(self.parents) if x<0]
    
    #グループの数を返す
    def group_count(self):
        return len(self.roots())
    
    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}
    
    def __str__(self):
        return "\n".join("{}: {}".format(r,self.members(r)) for r in self.roots())


if __name__  == "__main__":
    input = sys.stdin.readline
    n,m = map(int,input().split())
    UF = UnionFind(n)
    for i in range(m):
        a,b = map(int,input().split())
        UF.union(a,b)
    q = int(input())
    for i in range(q):
        c,d = map(int,input().split())
        if UF.same(c,d):
            print("yes")
        else:
            print("no")

Python: import sys
input_lines = [[int(i) for i in line.split()] for line in sys.stdin.read().splitlines()]
[user_num, rel_num] = input_lines[0]
rel_list = input_lines[1:rel_num+1]
ques_num = input_lines[rel_num+1][0]
ques_list = input_lines[rel_num+2:]
color_list = [-1] * user_num
adja_list = [set() for i in range(user_num)]

def dfs(id, color):
    if color_list[id] == -1:
        color_list[id] = color
        for a_id in adja_list[id]:
            dfs(a_id, color)



for rel in rel_list:
    adja_list[rel[0]].add(rel[1])
    adja_list[rel[1]].add(rel[0])

color = 0
for id in range(user_num):
    dfs(id, color)
    color += 1

for ques in ques_list:
    if color_list[ques[0]] == color_list[ques[1]]:
        print('yes')
    else:
        print('no')

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3

output:
yes
yes
no

"""

import sys


def assign_color():
    _color = 1
    for v in range(vertices):
        if color[v] == -1:
            graph_dfs(v, _color)
            _color += 1
    return None


def graph_dfs(vertex, _color):
    vertices_stack = list()
    vertices_stack.append(vertex)
    _color[vertex] = _color

    while vertices_stack:
        current_vertex = vertices_stack.pop()
        for v in adj_table[current_vertex]:
            if _color[v] == -1:
                _color[v] = _color
                vertices_stack.append(v)
    return None


def solve():
    for relation in relation_info:
        key, value = map(int, relation)
        adj_table[key].append(value)
        adj_table[value].append(key)

    assign_color()

    # print(adj_list)
    for question in questions:
        start, target = map(int, question)
        if color[start] == color[target]:
            print("yes")
        else:
            print("no")
    return color


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices, relations = map(int, _input[0].split())

    relation_info = map(lambda x: x.split(), _input[1:relations + 1])

    q_num = int(_input[relations + 1])
    questions = map(lambda x: x.split(), _input[relations + 2:])

    adj_table = tuple([] for _ in range(vertices))
    color = [-1] * vertices
    ans = solve()

Python: # -*- coding: utf-8 -*-
"""

"""

import sys
sys.setrecursionlimit(1000000)


n,m=[int(i) for i in input().split()]
graph=[[] for i in range(n)]

for i in range(m):
    relation=[int(j) for j in input().split()]
    graph[relation[0]].append(relation[1])
    graph[relation[1]].append(relation[0])


check=[0 for i in range(n)]
rabel=0
stack=[]

#深さ優先探索
def dfs(graph,i):
    global stack
    global n
    global rabel

    if check[i]==0:

        #行った足跡をつける
        check[i]=rabel

        #今いる場所の情報
        now=graph[i]

        #スタックに入れる
        stack.append(now)

        #今いる場所から到達できる地点のうち、まだ行っていないとこ全部でdfs
        for j in now:
            if check[j]==0:
                dfs(graph,j)

        stack.pop()

for i in range(n):
    if check[i]==0:
        rabel+=1
        dfs(graph,i)
# print(check)



num=int(input())
for i in range(num):
    a,b=[int(j) for j in input().split()]
    if check[a]==check[b]:
        print("yes")
    else:
        print("no")