Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func newGraph(nNodes int) *Graph {
	g := Graph{}
	g.nodes = make([]*GraphNode, nNodes)
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode] = &GraphNode{}
		g.nodes[iNode].key = GraphNodeKey(iNode)
	}
	return &g
}

var infty = 1000000000

type GraphNodeKey int

// GraphNode is a node for undirected graph
type GraphNode struct {
	key       GraphNodeKey
	adjacents []*GraphNode
	visited   bool
	d, f      int
	parent    *GraphNode
	cc        int
}

func (g *Graph) setAdjacent(l, r GraphNodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, g.nodes[r])
}

// Graph is a undirected graph
type Graph struct {
	nodes []*GraphNode
}

func (g *Graph) initialize() {
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode].visited = false
		g.nodes[iNode].d = infty
		g.nodes[iNode].f = infty
		g.nodes[iNode].parent = nil
		g.nodes[iNode].cc = -1 // 22.3-12
	}
}

type QueueEntry struct {
	node *GraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *GraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

func (q *Queue) isEmpty() bool {
	return q.head == nil
}

var time = 0

func fillDistancesByDepthFirstSearch(g *Graph) {
	g.initialize()
	time = 0
	cc := 0

	for _, u := range g.nodes {
		if !u.visited {
			dfsVisit(g, u, cc)
			cc++
		}
	}
}

func dfsVisit(g *Graph, u *GraphNode, cc int) {
	time++
	u.d = time
	u.cc = cc

	u.visited = true
	for _, v := range u.adjacents {
		if !v.visited {
			v.parent = u
			dfsVisit(g, v, cc)
		}
	}
	time++
	u.f = time
}

func main() {
	scanner.Split(bufio.ScanWords)

	nUsers := nextInt()
	nEdges := nextInt()
	graph := newGraph(nUsers)
	for iEdge := 0; iEdge < nEdges; iEdge++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())
		graph.setAdjacent(l, r)
		graph.setAdjacent(r, l)
	}

	fillDistancesByDepthFirstSearch(graph)

	nQueries := nextInt()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())

		// fmt.Println(graph.nodes[l].d, graph.nodes[r].d, graph.nodes[r].f, graph.nodes[l].f)
		if graph.nodes[l].cc == graph.nodes[r].cc {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

Python: n, m = map(int, input().split())
rootList = [-1 for i in range(n)]
 
def getRoot(x):
    r = rootList[x]
    if r < 0:
        rootList[x] = x
    elif r != x:
        rootList[x] = getRoot(r)
    return rootList[x]
 
 
def setSameRoot(x, y):
    x = getRoot(x)
    y = getRoot(y)
    if x != y:
        rootList[x] = rootList[y] = min(x, y)
 
for i in range(m):
    s, t = map(int, input().split())
    setSameRoot(s, t)
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print("yes" if getRoot(s) == getRoot(t) else "no")

Python: import sys
readline = sys.stdin.readline
write = sys.stdout.write
N, M = map(int, readline().split())
*p, = range(N)
def root(x):
    if x == p[x]:
        return x
    p[x] = x = root(p[x])
    return x
def unite(x, y):
    px = root(x); py = root(y)
    if px < py:
        p[py] = px
    else:
        p[px] = py


for i in range(M):
    s, t = map(int, readline().split())
    unite(s, t)
*_, = map(root, range(N))
Q = int(readline())
ans = []
for q in range(Q):
    s, t = map(int, readline().split())
    ans.append('yes' if p[s] == p[t] else 'no')
write("\n".join(ans))
write("\n")

Python: n,m = map(int,raw_input().strip().split(' '))
graph = [[] for i in range(n)]
for i in range(m):
    s,t = map(int,raw_input().strip().split(' '))
    graph[s].append(t)
    graph[t].append(s)
 
color = {}
c = 0
for i in range(n):
    if not i in color:
        que = [i]
        color[i] = c
        if len(que) > 0:
       #  for j in range(len(que)):
            u = que[0]
            for v in graph[u]:
                if not v in color:
                    color[v] = c
                    que.append(v)
            del que[0]
        c += 1

Python: # -*- coding: utf-8 -*-

def assignColor():
    global C
    for i in range(n):
        if C[i] == -1:
            C = BFS(i, C)

#@profile
def BFS(start, C):
    Q = [start]
    C[start] = start

    while len(Q) != 0:
        u = Q.pop(0)
        v = [i for i in G[u] if C[i] == -1] # v := ??£??\???????????????????????¢?´¢??????????????????
        
        for i in v:
            Q.append(i)
            C[i] = C[start]
    
    return C

if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    G = [[] for i in range(n)]
    C = [-1]*n
    
    for i in range(m):
        s, t = list(map(int, input().split()))
        G[s].append(t)
        G[t].append(s)
    assignColor()
    q = int(input())
    for i in range(q):
        s, t = list(map(int, input().split()))
        if C[s] == C[t]:
            print("yes")
        else:
            print("no")

Python: from collections import deque
from enum import Enum, auto

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

n, m = list(map(int, input().split(' ')))
adj_list = [[] for i in range(n)]
for i in range(m):
    u, v = list(map(int, input().split(' ')))
    adj_list[u].append(v)
    adj_list[v].append(u)

# 幅優先
def adj_bfs(s, e):
    queue = deque([])
    colors = [Color.WHITE for i in range(n)]

    queue.append(s)
    colors[s] = Color.GRAY

    while len(queue) > 0:
        u = queue.popleft()
        for v in adj_list[u]:
            if colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                queue.append(v)
                if v == e: return True
        colors[u] = Color.BLACK
    return False

# 深さ優先
colors = [Color.WHITE for i in range(n)]

def adj_dfs(u, e):
    colors[u] = Color.GRAY

    flag = False
    for v in adj_list[u]:
        if colors[v] == Color.WHITE:
            if v == e: return True
            flag = adj_dfs(v, e) or flag
            if flag: break

    colors[u] = Color.BLACK

    return flag

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

Python: n, m = map(int, input().split())
rootList = [-1 for i in range(n)]
 
def getRoot(x):
    r = rootList[x]
    if r < 0:
        rootList[x] = x
    elif r != x:
        rootList[x] = getRoot(r)
    return rootList[x]
 
 
def setSameRoot(x, y):
    x = getRoot(x)
    y = getRoot(y)
    if x != y:
        rootList[x] = rootList[y] = min(x, y)
 
for i in range(m):
    s, t = map(int, input().split())
    setSameRoot(s, t)
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print("yes" if getRoot(s) == getRoot(t) else "no")

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

g = {}
for i in range(m):
    s,t = map(int,input().split())
    val = g.get(s,[])
    val.append(t)
    g[s] = val
    val = g.get(t,[])
    val.append(s)
    g[t] = val

count = 0
def dfs(g, s, done=[]):
    global count
    count += 1
    if s in done:
        return
    done.append(s)
    for c in g[s]:
        dfs(g, c, done)
    return done

q = int(input())

n = set(g.keys())
cc = []
from sys import setrecursionlimit
setrecursionlimit(1500)
while n != set([]):
    t = n.pop()
    d = dfs(g, t, [])
    cc.append(d)
    n = n - set(d)

for j in range(q):
    s,t = map(int,input().split())
    k = True
    for c in cc:
        if s in c and t in c:
            print('yes')
            k = False
            break
    if k: print('no')