Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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)
    return group_num

A = range(n)
for i in A:
    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)

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

color = [0] * n
affil = [0] * n

group_num = -1
for i in range(n):
    if color[i] == 0:
        Q = [i]
        affil[i] = group_num + 1
        group_num = BFS(i, group_num + 1)

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


class UnionFind:
    def __init__(self, node_size):
        self._node = node_size
        self.par = [i for i in range(self._node)]
        self.rank = [0] * self._node
        self.size = [1] * self._node

    def find(self, ver):
        if self.par[ver] == ver:
            return ver
        else:
            self.par[ver] = self.find(self.par[ver])
            return self.par[ver]

    def unite(self, ver1, ver2):
        ver1, ver2 = self.find(ver1), self.find(ver2)
        if ver1 == ver2:
            return
        if self.rank[ver1] < self.rank[ver2]:
            ver1, ver2 = ver2, ver1
        self.par[ver2] = ver1
        self.size[ver1] += self.size[ver2]
        if self.rank[ver1] == self.rank[ver2]:
            self.rank[ver1] += 1

    def size(self, ver):
        ver = self.find(ver)
        return self.size[ver]

    def same(self, ver1, ver2):
        return self.find(ver1) == self.find(ver2)

if __name__ == '__main__':
    n, m = map(int, sys.stdin.readline().split())
    uf = UnionFind(n)
    for i in range(m):
        s, t = map(int, sys.stdin.readline().split())
        uf.unite(s, t)
    q = int(sys.stdin.readline())
    for i in range(q):
        s, t = map(int, sys.stdin.readline().split())
        if uf.same(s, t):
            print("yes")
        else:
            print("no")

Python: def bfs(graph, start, goal, connection):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        if now == goal or (1000000*now + goal) in connection:
            connection[1000000*start + goal] = 1
            return "yes"
        unvisited = unvisited[1:]
        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 = {}

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

q = int(input())

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

Python: import sys, collections
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = 10**10
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    n, m = LI()
    edge = [[] for _ in range(n)]
    cc = [-1]*n

    for _ in range(m):
        s, t = LI()
        edge[s].append(t)
        edge[t].append(s)

    ccid = 0
    for i in range(n):
        if cc[i]==-1:
            stk = [i]
            cc[i] = ccid
            while stk:
                c = stk.pop()
                for n in edge[c]:
                    if cc[n]==-1:
                        stk.append(n)
                        cc[n] = ccid

            ccid += 1

    q = I()
    for _ in range(q):
        s, t = LI()
        if cc[s] == cc[t]:
            print('yes')
        else:
            print('no')

if __name__ == '__main__':
    resolve()

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)

groups = [None] * n


def dfs(u, group_num):
    path = []
    path.append(u)
    groups[u] = group_num
    while path:
        u = path[-1]
        if adj[u]:
            v = adj[u].pop(0)
            if not groups[v]:
                groups[v] = group_num
                path.append(v)
        else:
            path.pop()


g_n = 1
for i in range(n):
    if not groups[i]:
        dfs(i, g_n)
        g_n += 1

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


q = int(input())

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

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

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

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

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

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

Python: def bfs(graph, start, goal, connection):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        if now == goal or connection[1000000*now + goal] == 1:
            connection[1000000*start + goal] = 1
            return "yes"
        unvisited = unvisited[1:]
        for next in graph[now][1:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

n, m = map(int, input().split())
g = [[i] for i in range(n)]
c = {}

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

q = int(input())

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