Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func newGraph(nNodes int) *Graph {
	g := Graph{}
	g.nodes = make([]*GraphNode, nNodes)
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode] = &GraphNode{}
		g.nodes[iNode].key = GraphNodeKey(iNode)
	}
	return &g
}

var infty = 1000000000

type GraphNodeKey int

// GraphNode is a node for undirected graph
type GraphNode struct {
	key       GraphNodeKey
	adjacents []*GraphNode
	visited   bool
	d, f      int
	parent    *GraphNode
	cc        int
}

func (g *Graph) setAdjacent(l, r GraphNodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, g.nodes[r])
}

// Graph is a undirected graph
type Graph struct {
	nodes []*GraphNode
}

func (g *Graph) initialize() {
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode].visited = false
		g.nodes[iNode].d = infty
		g.nodes[iNode].f = infty
		g.nodes[iNode].parent = nil
		g.nodes[iNode].cc = -1 // 22.3-12
	}
}

type QueueEntry struct {
	node *GraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *GraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

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

var time = 0

func fillDistancesByDepthFirstSearch(g *Graph) {
	g.initialize()
	time = 0
	cc := 0

	for _, u := range g.nodes {
		if !u.visited {
			dfsVisit(g, u, cc)
			cc++
		}
	}
}

func dfsVisit(g *Graph, u *GraphNode, cc int) {
	time++
	u.d = time
	u.cc = cc

	u.visited = true
	for _, v := range u.adjacents {
		if !v.visited {
			v.parent = u
			dfsVisit(g, v, cc)
		}
	}
	time++
	u.f = time
}

func main() {
	scanner.Split(bufio.ScanWords)

	nUsers := nextInt()
	nEdges := nextInt()
	graph := newGraph(nUsers)
	for iEdge := 0; iEdge < nEdges; iEdge++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())
		graph.setAdjacent(l, r)
		graph.setAdjacent(r, l)
	}

	fillDistancesByDepthFirstSearch(graph)

	nQueries := nextInt()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())

		// fmt.Println(graph.nodes[l].d, graph.nodes[r].d, graph.nodes[r].f, graph.nodes[l].f)
		if graph.nodes[l].cc == graph.nodes[r].cc {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

Python: import sys
 
def make_connected_group(G):
    C = [None]*len(G)
    group = 0
    for i,p in enumerate(C):
        if p != None: continue
        friends = [i]
        while len(friends) > 0:
            f = friends.pop()
            if C[f] == None:
                C[f] = group
                friends += G[f]
        group += 1
    return C
 
 
if __name__=='__main__':
    n,m = list(map(int,sys.stdin.readline().split()))
    G = [[] for _ in range(n)]
    for _ in range(m):
        s,t = list(map(int,input().split()))
        G[s].append(t)
        G[t].append(s)
    C = make_connected_group(G)
    q = int(input())
    for j,line in enumerate(sys.stdin):
        if not j < q: break
        s,t = list(map(int,line.split()))
        print('yes' if C[s] == C[t] else 'no')

Python: import sys
sys.setrecursionlimit(100000)

def make_connected_group(G):
    C = [-1]*len(G)
    def main(G,i,grp):
        if C[i] > -1: return
        C[i] = grp
        for j in G[i]: main(G,j,grp)
    group = 0
    for p,cp in enumerate(C): 
        if cp == -1:
            main(G,p,group)
            group += 1
    return C


if __name__=='__main__':
    n,m = list(map(int,input().split()))
    G = [[] for _ in range(n)]
    for i,line in enumerate(sys.stdin):
        if not i < m:
            q = int(line)
            break
        s,t = list(map(int,line.split()))
        G[s].append(t)
        G[t].append(s)
    cn = make_connected_group(G)
    for j,line in enumerate(sys.stdin):
        if not j < q: break
        s,t = list(map(int,line.split()))
        print('yes' if cn[s] == cn[t] else 'no')

Python: #!/usr/bin/python3

def set_color(i, ci, e, color):
    color[i] = ci
    for next_i in e[i]:
        if color[next_i] != ci:
            set_color(next_i, ci, e, color)

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

e = [set() for i in range(n)]
color = [None] * n

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

ci = 0
for i in range(n):
    if color[i] == None:
        set_color(i, ci, e, color)
        ci += 1

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

Python: 
def BFS(s = 0, group_num = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(A[s])):
        if color[A[s][j]] == 0:
            Q.append(A[s][j])
            color[A[s][j]] = 1
            affil[A[s][j]] = group_num
    if len(Q) != 0:
        BFS(Q[0], group_num)

n = 10
m = 9
friends = [[0, 1], [0, 2], [3, 4], [5, 7], [5, 6], [6, 7], [6, 8], [7, 8], [8, 9]]

A = range(n)
for i in A:
    A[i] = list()
for i in range(m):
    entry = friends[i]
    s = entry[0]
    t = entry[1]
    A[s].append(t)
    A[t].append(s)

color = [0] * n
affil = [0] * n
group_num = -1
for i in range(n):
    if color[i] == 0:
        Q = [i]
        group_num = group_num + 1
        affil[i] = group_num
        BFS(i, group_num)

q = 3
quest = [[0, 1], [5, 9], [1, 3]]
for i in range(q):
    entry = quest[i]
    if affil[entry[0]] == affil[entry[1]]:
        print("yes")
    else:
        print("no")

Python: 
def BFS(s = 0, group_num = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(A[s])):
        if color[A[s][j]] == 0:
            Q.append(A[s][j])
            color[A[s][j]] = 1
            affil[A[s][j]] = group_num
    if len(Q) != 0:
        BFS(Q[0], group_num)
    return group_num

A = range(n)
for i in A:
    A[i] = list()
for i in range(m):
    entry = map(int, raw_input())
    s = entry[0]
    t = entry[1]
    A[s].append(t)
    A[t].append(s)

entry = map(int, raw_input())
n = entry[0]
m = entry[1]

color = [0] * n
affil = [0] * n

group_num = -1
for i in range(n):
    if color[i] == 0:
        Q = [i]
        affil[i] = group_num + 1
        group_num = BFS(i, group_num + 1)

q = int(raw_input())
for i in range(q):
    entry = map(int, raw_input())
    if affil[entry[0]] == affil[entry[1]]:
        print("yes")
    else:
        print("no")