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: def dfs(G, C, id, color):
    S = []
    S.append(id)
    C[id] = color

    while S:
        u = S[-1]  # ?????¨?¨????????????????????????°??????
        S = S[:-1]  # pop
        for i in range(len(G[u])):
            v = G[u][i]
            if C[v] == -1:
                C[v] = color
                S.append(v)


if __name__ == '__main__':
    # ??????????????\???
    num_of_users, num_of_links = [int(x) for x in input().split(' ')]
    links = []
    for _ in range(num_of_links):
        links.append(list(map(int, input().split(' '))))
    num_of_queries = int(input())
    queries = []
    for _ in range(num_of_queries):
        queries.append(list(map(int, input().split(' '))))

    # ???????????????
    G = [[] for _ in range(11)]
    C = [-1] * 11
    for f, t in links:
        G[f].append(t)
        G[t].append(f)

    color = 1
    for id in range(num_of_users):
        if C[id] == -1:
            dfs(G, C, id, color)
        color += 1

    # ???????????????
    for x, y in queries:
        if C[x] == C[y]:
            print('yes')
        else:
            print('no')

Python: import sys


def dfs(u, col):
    global time
    color[u] = "GRAY"
    for v in range(n):
        if v in M[u] and color[v] == "WHITE":
            dfs(v, col)
    color[u] = col


if __name__ == "__main__":
    n, m = map(int, sys.stdin.readline().split())
    color = ["WHITE"] * n
    M = [[] for i in range(n)]
    inp = sys.stdin.readlines()
    for i in range(m):
        inp_i = list(map(int, inp[i].split()))
        M[inp_i[0]].append(inp_i[1])
        M[inp_i[1]].append(inp_i[0])
    for i in range(n):
        if color[i] == "WHITE":
            dfs(i, i)
    for i in range(int(inp[m])):
        inp_i = list(map(int, inp[i+m+1].split()))
        if color[inp_i[0]] == color[inp_i[1]]:
            print("yes")
        else:
            print("no")

Python: from collections import deque


def bfs(root):
    global time
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in adjDict[i]:
            if color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black'
        label[i] = time


firstline = list(map(int, input().split()))

n = firstline[0]
adjDict = {i: [] for i in range(n)}
color = ['white'] * n
distance = [-1] * n
label = [-1] * n

m = firstline[1]
for _ in range(m):
    adj = list(map(int, input().split()))
    adjDict[adj[0]].append(adj[1])
    adjDict[adj[1]].append(adj[0])

time = 0
for i in range(n):
    if color[i] == 'white':
        bfs(i)
        time += 1

q = int(input())
for _ in range(q):
    question = list(map(int, input().split()))
    if label[question[0]] == label[question[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

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

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)

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")

Python: import sys

def srch(i, end, l, Dst, Lng):
    if Lng[i] != -1 and Lng[i] < l:
        return
    Lng[i] = l
    if i == end:
        return
    if Dst[i] is not None and len(Dst[i]) > 0:
        for c in Dst[i]:
            srch(c, end, l+1, Dst, Lng)

def main():
    istr = sys.stdin.read()
    ilist = []
    for i in istr.splitlines():
        ilist.append(list(map(int,i.split())))
    #print(ilist)
    unum, fnum = ilist[0]

    Dst = [[] for i in range(unum)]
    fnum += 1
    for n in range(1, fnum):
        uid, fid = ilist[n]
        Dst[uid].append(fid)
        Dst[fid].append(uid)
    
    qnum = ilist[fnum][0]
    fnum += 1
    #print("fnum={} qnum={}".format(fnum,qnum))
    for q in range(fnum, (fnum + qnum)):
        id1, id2 = ilist[q]
        Lng = [-1] * (unum)
        #print("[{}] id1={:2} id2={:2} Dst:{} Lng:{}".format(q,id1, id2, Dst, Lng))
        srch(id1, id2, 0, Dst, Lng)
        if Lng[id2] != -1:
            print("yes")
        else:
            print("no")

if __name__ == '__main__':
    main()