Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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):
    data = M[person]
    for per in data:
        if per ==  target:
            global yes
            yes = 1
            return
        if color[per] == "white":
            color[per] = "gray"
            search(per, target)
    color[person] = "black"

num = int(input())
for i in range(num):
    yes = 0
    color = ["white" for j in range(n)]
    person, target = map(int,input().split())
    search(person, target)
    if yes == 0:
        print("no")
    else:
        print("yes")

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

import sys


def assign_color():
    _id = 1
    for m in range(vertices_num):
        if vertices_status_list[m] == -1:
            graph_dfs(m, _id)
            _id += 1
    return None


def graph_dfs(vertex, color):
    vertices_stack = list()
    vertices_stack.append(vertex)
    vertices_status_list[vertex] = color
    while vertices_stack:
        current_vertex = vertices_stack[-1]
        vertices_stack.pop()
        for v in adj_list[current_vertex]:
            if vertices_status_list[v] == -1:
                vertices_status_list[v] = color
                vertices_stack.append(v)
    return None


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

    relation_info = _input[1:relation_num + 1]

    question_num = int(_input[relation_num + 1])
    question_list = _input[relation_num + 2:]

    adj_list = [[] for _ in range(vertices_num)]
    vertices_status_list = [-1] * vertices_num

    for relation in relation_info:
        key, value = map(int, relation.split())
        adj_list[key].append(value)
        adj_list[value].append(key)

    assign_color()

    # print(adj_list)
    for question in question_list:
        start, target = map(int, question.split())
        if vertices_status_list[start] == vertices_status_list[target]:
            print("yes")
        else:
            print("no")

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

import sys

sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
INF=float('inf')

N,M=MAP()
nodes=[[] for i in range(N)]
for i in range(M):
    s,t=MAP()
    nodes[s].append(t)
    nodes[t].append(s)

group=[-1]*N
def rec(u, root):
    if group[u]!=-1:
        return
    group[u]=root
    for v in nodes[u]:
        rec(v, root)

for i in range(N):
    rec(i, i)

Q=INT()
for _ in range(Q):
    s,t=MAP()
    if group[s]==group[t]:
        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] * (fnum)
        #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()

Python: from collections import deque

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

V =[deque([]) for _ in range(n)]
color = [0 for _ in range(n)]

for _ in range(m):
    a,b = map(int,input().split())
    V[a].append(b)
    V[b].append(a)

friend = 1

def dfs(u,f):
    global color
    color[u] = f
    stack = []
    stack.append(u)
    while len(stack) != 0:
        now = stack[-1]
        # print(now,V,color)
        if len(V[now]) != 0:
            nb = V[now].popleft()
            if color[nb] == 0:
                stack.append(nb)
                color[nb] = f
        else :
            stack.pop()

# print("")

for i in range(n):
    if color[i]==0:
        dfs(i,friend)
        friend += 1

# print("")
# print(V)
# print(color)
q = int(input())
for _ in range(q):
    a,b = map(int,input().split())
    if color[a] == color[b]:
        print("yes")
    else:
        print("no")

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

color = [0] * n
affil = [0] * n
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 = friends[i]
    s = entry[0]
    t = entry[1]
    A[s].append(t)
    A[t].append(s)

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