Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

Python: def read_friends():
    n, m = [int(x) for x in input().split()]
    adj_list = [set() for _ in range(n)]

    for _ in range(m):
        s, t = [int(x) for x in input().split()]
        adj_list[s].add(t)
        adj_list[t].add(s)

    return n, adj_list


def divide_group(n, adj_list):
    group = [None] * n

    def visit(k, g):
        S = [k]

        while S:
            k = S.pop()
            group[k] = g
            for t in adj_list[k]:
                if group[t] is None:
                    S.append(t)

    for k in range(n):
        if group[k] is None:
            visit(k, k)

    return group


def answer_queries(group):
    q = int(input())

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


def main():
    n, adj_list = read_friends()
    group = divide_group(n, adj_list)
    answer_queries(group)


if __name__ == '__main__':
    main()

Python: def dfs(u):
    global ct
    if color[u]!=-1:
        return
    color[u]=ct
    for v in fr[u]:
        if v!=-1 and color[v]==-1:
            dfs(v)
    color[u]=ct
    
n,m=map(int,raw_input().split())
fr=[[] for i in xrange(n)]
for i in xrange(m):
    s,t=map(int,raw_input().split())
    fr[s].append(t)
    fr[t].append(s)

q=input()
ct=0
color=[-1]*n
for i in xrange(n):
    dfs(i)
    ct+=1
for i in xrange(q):
    s,t=map(int,raw_input().split())
    if color[s]==color[t]:
        print('yes')
    else:
        print('no')