Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

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)

entry = map(int, raw_input().split())
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().split())
    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 = int(raw_input())
for i in range(q):
    entry = map(int, raw_input().split())
    if affil[entry[0]] == affil[entry[1]]:
        print("yes")
    else:
        print("no")

Python: from collections import deque

def bfs(graph, start, goal, connection):
    visited = {start: None}
    unvisited = deque()
    unvisited.append(start)
    while unvisited:
        now = unvisited.popleft()
        if now == goal or goal in connection[now]:
            connection[start].add(goal)
            connection[goal].add(start)
            return "yes"
        connection[start].update(graph[now])
        for next in graph[now]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

n, m = map(int, input().split())
g = [[] for i in range(n)]
c = [set() 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])
    c[inp[0]].add(inp[1])
    c[inp[1]].add(inp[0])

q = int(input())

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

Python: from collections import deque

def connected_components(U, C):
    no = 1

    for i in range(len(U)):
        if C[i] == None:
            depth_first_serch(U, C, i, no)
            no += 1

def depth_first_serch(U, C, r, no):
    S = list()
    S.append(r)

    while len(S) != 0:
        current = S[-1]
        
        if len(U[current]) != 0:
            v = U[current].popleft()
            if C[v] == None:
                C[v] = no
                S.append(v)
        else:
            C[current] = no
            S.pop()

def Main():
    N, M = map(int, input().split())
    Users = [deque() for n in range(N)]
    Color = [None for n in range(N)]

    for m in range(M):
        s, t = map(int, input().split())
        Users[s].append(t)
        Users[t].append(s)

    connected_components(Users, Color)

    Q = int(input())

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

Main()

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)

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

A = [0] * n
for i in range(n):
    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]
        group_num = group_num + 1
        affil[i] = group_num
        BFS(i, group_num)

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: # -*- coding: utf-8 -*-
"""
Created on Sat Apr  6 14:09:17 2019

@author: Yamazaki Kenichi
"""
import sys
sys.setrecursionlimit(10**6)

n,m = map(int,input().split())
A = [list(map(int,input().split())) for i in range(m)]
q = int(input())
Q = [list(map(int,input().split())) for i in range(q)]

class UnionFind(object):
    def __init__(self,size):
        self.table = [-1 for i in range(size)]
    def find(self,x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self.find(self.table[x])
            return self.table[x]
    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1] <= self.table[s2]:
                self.table[s1] += self.table[s2]
                self.table[s2] = s1
            else:
                self.table[s2] += self.table[s1]
                self.table[s1] = s2
            return True
        else:
            return False
    def check(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            return False
        else:
            return True

uf = UnionFind(n)
for c in A:
    uf.union(c[0],c[1])
for c in Q:
    if uf.check(c[0],c[1]):
        print('yes')
    else:
        print('no')

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

# 隣接リストの雛形
D = dict()
for i in range(n):
    D[i] = []

# グラフを入力
for _ in range(m):
    s, t = map(int, input().split())
    D[s].append(t)
    D[t].append(s)

group = [-1]*n
cnt = 0
for i in range(n):
    if group[i] == -1:
        group[i] = cnt
        stack = [i]
        while stack:
            v = stack.pop()
            for c in D[v]:
                if group[c] == -1:
                    group[c] = cnt
                    stack.append(c)
        cnt += 1

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

Python: def DFS(s, j):
    group[s] = j
    for i in range(n):
        if i in G[s] and group[i] == 0:
            DFS(i, j)

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

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

group = [0 for i in range(n)]
j = 1
for i in range(n):
    if group[i] == 0:
        DFS(i)
        j += 1

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