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 dfs(r, c):
    global color, G
    S = [r]
    color[r] = c
    while (0 < len(S)):
        u = S.pop()
        for u in G[u]:
            v = color[u]
            if (v == None):
                color[u] = c
                S.append(u)
 
def assignColor():
    global color, G, n
    c = 1
    for i in range(n):
        if (color[i] == None):
            dfs(i, c)
            c += 1
 
n, m = list(map(int, input().split()))
 
G = [None 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: 
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: n, m = map(int, input().split())

adj = [[] for i in range(n)]

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

isVisited = [False] * n

CC = [None] * n

import sys
sys.setrecursionlimit(100000)

def dfs(u, group):
    isVisited[u] = True
    CC[u] = group
    for v in adj[u]:
        if not isVisited[v]:
            dfs(v, group)

def makeCC(group):
    for i in range(n):
        if not isVisited[i]:
            dfs(i, group)
            group += 1

def isConnected(v1, v2):
    if CC[v1] == CC[v2]:
        print('yes')
    else:
        print('no')


makeCC(0)


q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    isConnected(s, t)

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(len(que)):
            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(" "))

rank = [1]*n
dad = [i for i in range(n)]

def find(a):
    if a == dad[a]:
        return a
    else:
        dad[a] = find(dad[a])
        return dad[a]

for _ in range(m):
    a,b = map(int,input().split(" "))

    #find parents
    aP = find(a)
    bP = find(b)
    if aP == bP:
        continue

    aR = rank[aP]
    bR = rank[bP]

    if aR >= bR:
        dad[bP] = aP
        if rank[aP] == rank[bP]:
            rank[aP] += 1
            rank[bP] = rank[aP]
    else:
        dad[aP] = bP
        if rank[aP] == rank[bP]:
            rank[bP] += 1
            rank[aP] = rank[bP]

qN = int(input())
for _ in range(qN):
    a,b = map(int,input().split(" "))
    if find(a) == find(b):
        print("yes")
    else:
        print("no")

Python: from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


V,E = map(int,input().split())

boss = [i for i in range(V)]
height = [0 for i in range(V)]

def get_boss(index):
    if index == boss[index]:
        return index
    else:
        boss[index] = get_boss(boss[index])
        return boss[index]


def is_same_group(a,b):
    return get_boss(a) == get_boss(b)

def unite(x,y):
    boss_x = get_boss(x)
    boss_y = get_boss(y)

    if boss_x == boss_y:
        return

    if height[x] > height[y]:

        boss[boss_y] = boss_x

    elif height[x] < height[y]:

        boss[boss_x] = boss_y

    else:
        boss[boss_y] = boss_x
        height[x] += 1


for loop in range(E):
    A,B = map(int,input().split())
    unite(A,B)

num_query = int(input())

for loop in range(num_query):
    A,B = map(int,input().split())
    if is_same_group(A,B) == True:
        print("yes")
    else:
        print("no")

Python: def scanu(t):
    a = list(map(t,input().split()))
    return a

def breathSerch(P,N,start,end):
    n=len(P)-1
    m=1
    QQ=[N[start]]
    while(QQ != []):
        R=[]
        for Q in QQ:
            for i in range(1,n+1):
                if Q[i]==1 and P[i]==-1:
                    if i == end:
                        return True
                    P[i]=m
                    R.append(N[i])

        QQ = R
        m=m+1
    return False

(n,m) = scanu(int)
A = [[0 for j in range(n+1)] for i in range(n+1)]
for i in range(m):
    (s,t)= scanu(int)
    A[s+1][t+1]=1
    A[t+1][s+1]=1

q = int(input())

for i in range(q):
    (s,t) = scanu(int)
    P=[-1 for i in range(n+1)]
    P[s+1]=0
    res = breathSerch(P,A,s+1,t+1)
    if res or P[t+1]!=-1:
        print("yes")
    else:
        print("no")

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


if __name__ == '__main__':

    n, m = [int(s) for s in input().split(" ")]
    M = [set() for j in range(n)]
    for _ in range(m):
        u, v = [int(s) for s in input().split(" ")]
        M[u].add(v)
        M[v].add(u)
        
    color = [0] * n
    def dfs(u, c):  # depth-first search
        color[u] = c
        for v in M[u]:
            if color[v] == 0:
                dfs(v, c)
    
    c = 1
    for u in range(n):
        if color[u] == 0:
            dfs(u, c)
            c += 1
    
    l = int(input())
    for _ in range(l):
        p, q = [int(s) for s in input().split(" ")]
        if color[p] == color[q]:
            print("yes")
        else:
            print("no")