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: def connect(o,s,t):
    global ct
    if color[s]!="white" or ct!=0:
        return
    if fr[o][t]==t:
        ct+=1
    color[s]="grey"
    for i in fr[s]:
        if i==-1:
            continue
        elif i==t or ct!=0:
             ct+=1
             break
        else:
            fr[o][i]=i
            fr[i][o]=o
            connect(o,i,t)
            
n,m=map(int,raw_input().split())
fr=[[-1]*n for i in xrange(n)]
for i in xrange(m):
    s,t=map(int,raw_input().split())
    fr[s][t]=t
    fr[t][s]=s
q=input()
for i in xrange(q):
    ct=0
    color=["white"]*n
    s,t=map(int,raw_input().split())
    connect(s,s,t)
    if ct==0:
        print('no')
    else:
        print('yes')
        fr[s][t]=t
        fr[t][s]=s

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

a = [[] for i in range(n)]

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

q = int(input())

from collections import deque
queue = deque()


for j in range(q):
    memo = [0 for i in range(n)]
    data = list(map(int,input().split()))
    memo[data[0]] = 1
    queue.append(data[0])

    try:
        while len(queue) != 0:
            b = queue.popleft()
            if len(a[b]) != 0:
                for k in a[b]:
                    if memo[k] == 0:
                        memo[k] = 1
                        queue.append(k)
                        if k == data[1]:
                            print("yes")
                            raise Exception
        print("no")
    except Exception:
        pass

Python: #coding:utf-8

def CC(root,dist):
    for i in range(len(dist)):
        if i==root[0]:
            dist[i]=dist[i]+[root[1]]
        elif i==root[1]:
            dist[i]=dist[i]+[root[0]]
        
def Check(dist,start,end,clist):
    ans="no"
    if len(dist[end])!=0:
        if start in dist[end]:
            ans="yes"
        else:
            clist.append(end)
            for d in dist[end]:
                if d!=end and d not in clist:
                    ans=Check(dist,start,d,clist)
    return ans
    
if __name__=="__main__":
    I=input()
    n=int(I.split(" ")[0])
    m=int(I.split(" ")[1])
    A=[]
    for i in range(m):
        A.append(list(map(int,input().split(" "))))
    dist=[[]]*n
    for i in range(m):
        CC(A[i],dist)
    q=int(input())
    for i in range(q):
        qList=list(map(int,input().split(" ")))
        print(Check(dist,qList[0],qList[1],[]))

Python: def bfs(graph, start, goal):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        if now == goal:
            return "yes"
        unvisited = unvisited[1:]
        for next in graph[now][1:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

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

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

q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    print(bfs(g, s, t))

Python: #coding:utf-8
n, r = map(int,input().split())
Q = []
M = [[] for j in range(n)]
for i in range(r):
    i,j = map(int,input().split())
    M[i].append(j)
    M[j].append(i)


def search(person, target):
    Q = []
    Q.append(person)
    color = [0 for i in range(n)]
    while Q != []:
        u = Q.pop(0)
        if u ==  target:
            print("yes")
            break
        color[u] = 1
        data = M[u]
        for v in data:
            if color[v] == 0:
                color[v] = 1
                Q.append(v)
        color[u] = 2
    else:
        print("no")

num = int(input())
for i in range(num):
    person, target = map(int,input().split())
    search(person, target)

Python: class ConnectedComponents():
    def __init__(self, n):
        self.size = [1] * n
        self.id = [i for i in range(n + 1)]

    def root(self, i):
        while i != self.id[i]:
            self.id[i] = self.id[self.id[i]]
            i = self.id[i]

        return i

    def relations(self, s, t):
        s_root = self.root(s)
        t_root = self.root(t)

        if s_root == t_root: 
            return

        if self.size[s_root] < self.size[t_root]:
            self.id[s_root] = t_root
            self.size[t_root] += self.size[s_root]
        else:
            self.id[t_root] = s_root
            self.size[s_root] += self.size[t_root]

    def find(self, s, t):
        return self.root(s) == self.root(t)

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

CC = ConnectedComponents(n)

for _ in range(m):

    s, t = map(int, input().split())

    CC.relations(s, t)

# QUESTION
q = int(input())

for _ in range(q):

    s, t = map(int, input().split())

    if CC.find(s, t):
        print("yes")
    else:
        print("no")

Python: from collections import defaultdict


n, m = map(int, input().split())
rel = defaultdict(set)

for _ in range(m):
    p, r = map(int, input().split())
    rel[p].add(r)
    rel[r].add(p)


color = defaultdict(int)

def search(f, c):
    stack = [f]
    color[f] = c
    while stack:
        u = stack.pop()
        for v in rel[u]:
            if not color[v]:
                color[v] = c
                stack.append(v)


# assign color
cl = 0
for i in range(n):
    if not color[i]:
        cl += 1
        search(i, cl)

qn = int(input())

for _ in range(qn):
    f, l = map(int, input().split())
    print(['no', 'yes'][color[f] == color[l]])