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: import sys
import os
import pprint

#fd = os.open('ALDS1_11_D-3.txt', os.O_RDONLY)
#os.dup2(fd, sys.stdin.fileno())

"""connected components"""

l = list(map(int, input().split()))
DEBUG = False

# node num
n = l[0]

# relation num
m = l[1]

# each node color
colors = [None] * n

rel_list = []
for _ in range(n):
    rel_list.append([])
# relation
for _ in range(m):
    l = list(map(int, input().split()))
    s = l[0]
    t = l[1]
    rel_list[s].append(t)
    rel_list[t].append(s)

if DEBUG:
    print(rel_list)

# coloring by DFS or BFS
for node_index in range(n):

    stack = []
    if colors[node_index] is None:
        stack.append(node_index)
        while stack:
            # process top of stack
            top = stack.pop()
            colors[top] = node_index

            relations = rel_list[top]
            for relation in relations:
                if colors[relation] is None:
                    stack.append(relation)

if DEBUG:
    print(colors)

# question num
q = int(input())

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

Python: def dfs(r, c):
    global color
    global g
    s = [r]
    color[r] = c
    while len(s) > 0:
        u = s.pop()
        for u in g[u]:
            v = color[u]
            if v is None:
                color[u] = c
                s.append(u)
                
def assigncolor():
    global g
    global color
    global 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 = [[] 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 = [None 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: import sys
def s():
 e=sys.stdin.readline
 n,m=map(int,e().split())
 P=[[]for _ in[0]*n]
 C=[0]*n
 k=0
 for _ in[0]*m:
  s,t=map(int,e().split());P[s]+=[t];P[t]+=[s]
 for i in range(n):
  k+=1
  if C[i]<1:
   s=[i];C[i]=k
   while s:
    u=s.pop()
    for v in P[u]:
     if C[v]<1:C[v]=k;s+=[v]
 for _ in[0]*int(e()):
  a,b=map(int,e().split())
  print(['no','yes'][C[a]==C[b]])
if'__main__'==__name__:s()

Python: #coding: utf-8
n, m = map(int,input().split())
List = [[] for i in range(n)]
color = ["white" for i in range(n)]

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


def search(a,target):
    for member in List[a]:
        if member == target:
            print("yes")
            return True
        if color[member] == "white":
            color[member] = "black"
            return search(member, target)

p = int(input())
for i in range(p):
    a, target = map(int,input().split())
    b = search(a, target)
    if b != True:
        print("no")

Python: 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: from collections import deque

c=1        #cc count

def dfs(t):
    s=deque()
   
    global c

    s.append(t)
    color[t] = c
    while (len(s)!=0): 
        u = s.pop()
        lens = len(G[u])
        for i in range(lens) :
            v = G[u][i]
            if (flag[v] == 0) :
                color[v] = c
                flag[v] = 1
                s.append(v)
            
            
n,ln=[int(x) for x in input().split()]
maxx=n+10
m=[[0 for col in range(maxx)] for row in range(maxx)] 
G=[[] for row in range(maxx)]
color=[0 for col in range(maxx)]
flag=[0 for col in range(maxx)]



for i in range(ln):
    u,v=[int(x) for x in input().split()]
    G[u].append(v)
    G[v].append(u)

for i in range(n):
    if (flag[i] == 0):
            dfs(i)
            c+=1
        
q=int(input())
for i in range(q):
    u,v=[int(x) for x in input().split()]
    if (color[u] == color[v]):
        print("yes")
    else:
        print("no")

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

def breathSerch(P,N,start):
    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:
                    P[i]=m
                    R.append(N[i])
        QQ = R
        m=m+1

(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
    breathSerch(P,A,s+1)
    if P[t+1]!=-1:
        print("yes")
    else:
        print("no")

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

def are_connected(s, t):
    q = [s]
    checked = [False] * n
    checked[s] = True

    while q:
        user = q.pop(0)
        for f in friend[user]:
            if f == t:
                return True
            elif not checked[f]:
                q.append(f)
                checked[f] = True
    return False

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