Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

Python: import sys
sys.setrecursionlimit(10**6)

class Union_find():
    
    def __init__(self,n):
        self.n=n
        self.root=[-1]*(n+1)
        self.rank=[0]*(n+1)

    def find_root(self,x):# 頂点xのroot(根)頂点を見つける
        if self.root[x]<0:
            return x
        else:
            self.root[x]=self.find_root(self.root[x])
            return self.root[x]

    def unite(self,x,y):#頂点xと頂点yを繋ぐ辺を追加
        x=self.find_root(x)
        y=self.find_root(y)

        if x==y:
            return 
        elif self.rank[x]>self.rank[y]:
            self.root[x]+=self.root[y]
            self.root[y]=x
        else:
            self.root[y]+=self.root[x]
            self.root[x]=y

            if self.rank[x]==self.rank[y]:
                self.rank[y]+=1

    def same(self,x,y):#頂点xと頂点yが連結であるか判定
        return self.find_root(x)==self.find_root(y)

    def count(self,x):#頂点xが属する木のサイズ
        return -self.root[self.find_root(x)]

n,m=map(int,input().split())
gragh=Union_find(n+1)
for i in range(m):
    a,b=map(int,input().split())
    gragh.unite(a,b)
q=int(input())
for i in range(q):
    s,t=map(int,input().split())
    print("yes" if gragh.same(s,t) else "no")

Python: n, m = map(int, input().split())
relations = [[] for _ in range(n)]
 
for i in range(m):
    s, t = map(int, input().split())
    relations[s] += [t]
    relations[t] += [s]
friend_id = [None] * n
for s, t in enumerate(relations):
    if t:
        queue = [s]
        while len(queue):
            k = queue.pop()
            friend_id[k] = s
            queue += relations[k]
            relations[k] = []
 
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print('yes' if friend_id[s] != None and friend_id[s] == friend_id[t] else'no')

Python: def dfs(r, c):
    global c, color
    S = [r]
    color[r] = c
    while (0 < len(S)):
        u = S.pop()
        for u in G[u]:
            v = color[ch]
            if (v == False):
                color[u] = c
                S.append(u)
 
def assignColor():
    global c, color
    c = 1
    for i in range(n):
        if (color[i] == False):
            dfs(i, c)
            c += 1
 
n, m = list(map(int, input().split()))
 
G = [False for i in range(n)]
for i in range(m):
    s, t = list(map(int, input().split()))
    G[s].append(t)
    G[t].append(s)
 
color = [False for i in range(n)]
 
assignColor()
 
q = int(input())

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

Python: n,m = map(int,raw_input().strip().split(' '))
graph = [[] for i in range(n)]
for i in range(m):
    s,t = map(int,raw_input().strip().split(' '))
    graph[s].append(t)
    graph[t].append(s)
 
color = {}
c = 0
for i in range(n):
    if not i in color:
        que = [i]
        color[i] = c
        if len(que) > 0:
         for j in range (m):
            u = que[0]
            for v in graph[u]:
                if not v in color:
                    color[v] = c
                    que.append(v)
            del que[0]
        c += 1

Python: n, m = map(int, input().split())
# print('n:', n, 'm:', m)
A = [[0 for i in range(n)] for j in range(n)]
visited = [0 for i in range(n)]

for i in range(m):
    u, v = list(map(int, input().split()))
    # print(u, v)
    A[u][v] = 1
    A[v][u] = 1
# print(A)

q = int(input())
# print('q:', q)


def dfs(s):
    # print('visit', s)
    if s is None:
        return
    visited[s] = 1
    for v in range(n):
        # print('candidate:', v)
        if A[s][v] == 1 and visited[v] == 0:
            dfs(v)

def can_go(s, t):
    # print(s, '->', t, '----------')
    global visited
    visited = [0 for i in range(n)]
    dfs(s)
    # print(visited)
    if visited[t] == 1:
        return True
    else:
        return False

for i in range(q):
    s, t = map(int, input().split())
    if can_go(s, t):
        print('yes')
    else:
        print('no')

Python: import sys
sys.setrecursionlimit(10000)
def dfs(s):
    global t
    if s == t:
        return True
    DP[s]=1
    for i in F[s]:
        if not DP[i]:
            if dfs(i):
                return True
    return False
    
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())
for i in range(q):
    DP = [0]*n
    s,t = [int(i) for i in input().split()]
    if dfs(s):
        print("yes")
    else:
        print("no")

Python: class Vertex:
	def __init__(self):
		self.c = 0
		self.vertices = []
	def add(self, u):
		self.vertices.append(u)

def dfs(vertices):
	c = 0
	for i in range(len(vertices)):
		c += 1
		if vertices[i].c == 0:
			vertices[i].c = c
			stack = []
			stack.append(i)
			while len(stack) != 0:
				cur = vertices[stack.pop()]
				for j in range(len(cur.vertices)):
					u = cur.vertices[j]
					if vertices[u].c == 0:
						vertices[u].c = c
						stack.append(u)
	return

n, m = map(int, input().split())
vertices = []
for i in range(n):
	vertices.append(Vertex())
for i in range(m):
	u1, u2 = map(int, input().split())
	vertices[u1].add(u2)
	vertices[u2].add(u1)

dfs(vertices)

q = int(input())
for i in range(q):
	u1, u2 = map(int, input().split())
	if vertices[u1].c == vertices[u2].c:
		print('yes')
	else:
		print('no')

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


class LinkedList:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def check(self, start, goal):
        states = [0] * self.n
        queue = deque([start])

        while queue:
            u = queue.popleft()
            if states[u]:
                continue
            else:
                states[u] = 1

            for v in self.ll[u]:
                if v == goal:
                    print('yes')
                    return

                if states[v]:
                    continue
                else:
                    queue.append(v)

        else:
            print('no')
            return


if __name__ == '__main__':
    n, m = map(int, input().split())
    ll = LinkedList(n)

    for _ in range(m):
        u, v = map(int, input().split())
        ll.link(u, v)

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        ll.check(u, v)