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: n, m = map(int, input().split())
# print('n:', n, 'm:', m)
A = [[0 for i in range(n)] for j in range(n)]
visited = [0 for i in range(n)]

for i in range(m):
    u, v = list(map(int, input().split()))
    # print(u, v)
    A[u][v] = 1
    A[v][u] = 1
# print(A)

q = int(input())
# print('q:', q)


def dfs(s):
    # print('visit', s)
    if s is None:
        return
    visited[s] = 1
    for v in range(n):
        # print('candidate:', v)
        if A[s][v] == 1 and visited[v] == 0:
            dfs(v)

def can_go(s, t):
    # print(s, '->', t, '----------')
    global visited
    visited = [0 for i in range(n)]
    dfs(s)
    # print(visited)
    if visited[t] == 1:
        return True
    else:
        return False

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

Python: import sys
sys.setrecursionlimit(10000)
def dfs(s):
    global t
    if s == t:
        return True
    DP[s]=1
    for i in F[s]:
        if not DP[i]:
            if dfs(i):
                return True
    return False
    
n,m = [int(i) for i in input().split()]
F = [{} for i in range(n)]
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
for i in range(q):
    DP = [0]*n
    s,t = [int(i) for i in input().split()]
    if dfs(s):
        print("yes")
    else:
        print("no")

Python: class Vertex:
	def __init__(self):
		self.c = 0
		self.vertices = []
	def add(self, u):
		self.vertices.append(u)

def dfs(vertices):
	c = 0
	for i in range(len(vertices)):
		c += 1
		if vertices[i].c == 0:
			vertices[i].c = c
			stack = []
			stack.append(i)
			while len(stack) != 0:
				cur = vertices[stack.pop()]
				for j in range(len(cur.vertices)):
					u = cur.vertices[j]
					if vertices[u].c == 0:
						vertices[u].c = c
						stack.append(u)
	return

n, m = map(int, input().split())
vertices = []
for i in range(n):
	vertices.append(Vertex())
for i in range(m):
	u1, u2 = map(int, input().split())
	vertices[u1].add(u2)
	vertices[u2].add(u1)

dfs(vertices)

q = int(input())
for i in range(q):
	u1, u2 = map(int, input().split())
	if vertices[u1].c == vertices[u2].c:
		print('yes')
	else:
		print('no')

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


class LinkedList:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def check(self, start, goal):
        states = [0] * self.n
        queue = deque([start])

        while queue:
            u = queue.popleft()
            if states[u]:
                continue
            else:
                states[u] = 1

            for v in self.ll[u]:
                if v == goal:
                    print('yes')
                    return

                if states[v]:
                    continue
                else:
                    queue.append(v)

        else:
            print('no')
            return


if __name__ == '__main__':
    n, m = map(int, input().split())
    ll = LinkedList(n)

    for _ in range(m):
        u, v = map(int, input().split())
        ll.link(u, v)

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        ll.check(u, v)

Python: from collections import deque

n, m = map(int, input().split())
g = [set() for _ in range(n)]

while m:
    s, t = map(int, input().split())
    g[s].add(t)
    g[t].add(s)
    m -= 1


def find(s, t):
    global g
    queue = deque()
    queue.append((0, s))
    queue.append((1, t))
    visited = (set(), set())
    visited[0].add(s)
    visited[1].add(t)

    def remember(visited, new):
        g[new] |= visited
        for v in visited:
            g[v].add(new)
        visited.add(new)

    while queue:
        st, i = queue.popleft()
        for f in g[i]:
            if f in visited[not st]:
                return True
            if f not in visited[st]:
                remember(visited[st], f)
                queue.append((st, f))
    return False


q = int(input())
while q:
    s, t = map(int, input().split())
    print('yes' if find(s, t) else 'no')
    q -= 1

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

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

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

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

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

Python: from collections import deque
 
n, m = map(int, input().split())

adj = [[] for _ in range(n)] # adjacent list

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

c = ['w']*n # color of each vertex
con = [0]*n
cnt = 0


def BFS_visit(s):
    global c
    global cnt
    global con
    
    c[s] = 'g'
    con[s] = cnt
    
    Q = deque()
    Q.append(s)
    while Q:
        u = Q.popleft()
        for v in adj[u]:
            if c[v] == 'w':
                c[v] = 'g'
                con[v] = cnt
                Q.append(v)
        c[u] = 'b'
        
for u in range(n):
    if c[u] == 'w':
        cnt += 1
        BFS_visit(u)
        
        
q = int(input())
for _ in range(q):
    u, v = map(int, input().split())
    ans = "yes" if (con[u] == con[v]) else "no"
    print(ans)

Python: def main():
    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)

    q = int(input())
    component_ids = [-1 for i in range(n)]
    id = 0
    for i in range(n):
        if component_ids[i] != -1:
            continue
        bfs(i, id, adj, component_ids)
        id += 1

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


def bfs(node, id, adj, component_ids):
    que = [node]
    while que:
        u = que.pop(0)
        component_ids[u] = id
        for v in adj[u]:
            if component_ids[v] == -1:
                que.append(v)

main()