Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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

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

color = [None] * n
S = []
def dfs(r, c):
 S.append(r)
 color[r] = c
 while len(S) != 0:
  u = S.pop()
  for i in SNS[u]:
   if color[i] == None:
    color[i] = c
    S.append(i)
id = 0
for i in range(n):
 if color[i] == None:
  id += 1
  dfs(i, id)

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

Python: n, m = map(int, raw_input().split())
G = [[0 for i in range(n)] for j in range(n)]

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

for i in range(n):
    for j in range(n):
        for k in range(j+1, n):
            if G[j][k] == 1:
                for l in range(n):
                    if G[k][l] == 1:
                        G[j][l] = 1
                        G[l][j] = 1

q = int(raw_input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, raw_input().split())
    if G[s][t] == 1:
        ans[i] = "yes"
for i in range(q):
    print(ans[i])

Python: def connect(o,s,t):
    global ct
    if color[s]!="white" or ct==1:
        return
    if fr[o][t]==t:
        ct+=1
    color[s]="grey"
    for i in fr[s]:
        if i==-1:
            continue
        elif i==t:
             ct+=1
             break
        else:
            fr[o][i]=i
            fr[i][o]=s
            connect(o,i,t)
            
n,m=map(int,raw_input().split())
fr=[[-1]*n for i in xrange(n)]
for i in xrange(m):
    s,t=map(int,raw_input().split())
    fr[s][t]=t
    fr[t][s]=s
q=input()
for i in xrange(q):
    ct=0
    color=["white"]*n
    s,t=map(int,raw_input().split())
    connect(s,s,t)
    if ct==0:
        print('no')
    else:
        print('yes')
        fr[s][t]=t
        fr[t][s]=s

Python: import sys
sys.setrecursionlimit(10000)
n, m = map(int, input().split())
graph = [[] for _ in range(n)]

global visited 
visited = [0 for _ in range(n)]

def input_graph(m):
  for _ in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

def dfs(node_id):
  
  for ad_node in graph[node_id]:
    if visited[ad_node] == 1:
      continue
    visited[ad_node] = 1
    dfs(ad_node)

if __name__ == "__main__":
  input_graph(m)
  q_num = int(input())
  for _ in range(q_num):
    visited = [0 for _ in range(n)]
    s, t = map(int, input().split())
    dfs(s)
    if visited[t]:
      print("yes")
    else:
      print("no")

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

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

for i in range(m):
    com = list(map(int, input().split()))
    a[com[0]].append(com[1])
    a[com[1]].append(com[0])

q = int(input())

from collections import deque
for j in range(q):
    queue = deque()
    memo = [0 for i in range(n)]
    data = list(map(int, input().split()))
    memo[data[0]] = 1
    queue.append(data[0])

    try:
        while len(queue):
            b = queue.popleft()
            for k in a[b]:
                if k == data[1]:
                    print("yes")
                    raise Exception
                if memo[k] == 0:
                    memo[k] = 1
                    queue.append(k)
        print("no")
    except Exception:
        pass

Python: class UnionFind:
    def __init__(self, n: int):
        self.nodes = n
        self.parents = [-1] * n
        self.rank = [0] * n

    # retrun the root of element x
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    # unite the group include element x and group include element y
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parents[y] += self.parents[x]
            self.parents[x] = y
        else:
            self.parents[x] += self.parents[y]
            self.parents[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    # get size of the gourp which element x belongs
    def get_size(self, x):
        return -self.parents[self.find(x)]

    # check if element x and element y is same group
    def same(self, x, y):
        return self.find(x) == self.find(y)


n, m = [int(i) for i in input().split()]
union = UnionFind(n)
for _ in range(m):
    s, t = [int(i) for i in input().split()]
    union.unite(s, t)

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