Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

Python: def main():
    class UnionFind:
        def __init__(self, n=0):
            self.d = [-1]*n

        def root(self, x):
            if self.d[x] < 0:
                return x
            self.d[x] = self.root(self.d[x])
            return self.d[x]

        def unite(self, x, y):
            x, y = self.root(x), self.root(y)
            if x == y:
                return False
            if x > y:
                x, y = y, x
            self.d[x] += self.d[y]
            self.d[y] = x
            return True

        def same(self, x, y):
            return self.root(x) == self.root(y)

    raw = tuple(map(int, open(0).read().split()))
    n, m = raw[:2]
    uf = UnionFind(n)
    [uf.unite(a, b) for a, b in zip(*[iter(raw[2:2*m+2])]*2)]

    [print('yes' if uf.same(a, b) else 'no') for a, b in zip(*[iter(raw[2*m+3:])]*2)]

if __name__ == '__main__':
    main()

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()
            if len(a[b]):
                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: # -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 10:14:13 2018
ALDS1_11_D
@author: maezawa
"""
import sys
sys.setrecursionlimit(1000000)

n, m = list(map(int, input().split()))
v = [-1 for _ in range(n)]
stack = []
adj = [[] for _ in range(n)]
for i in range(m):
    s, t = list(map(int, input().split()))
    adj[s].append(t)
    adj[t].append(s)
#print(*adj, sep='\n')
def dfs(u, color):
    if v[u] > 0:
        return 0
    v[u] = color
    #print(u, current, stack)
    for k in adj[u]:
        if v[k] > 0:
            continue
        dfs(k, color)
    return 1

color = 1
for start in range(n):
    if dfs(start, color) == 1:
        color += 1
#for i in range(n):        
#    print('color:{} for node:{}'.format(i, v[i]))

q = int(input())
for i in range(q):
    s, t = list(map(int, input().split()))
    if v[s] == v[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
            connection[1000000*goal + start] = 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])
    c[1000000*inp[0] + inp[1]] = 1
    c[1000000*inp[1] + inp[0]] = 1

q = int(input())

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

Python: def DFS(s):
    flag[s] = 1
    F[group] = F[group] + s
    for i in range(n):
        if G[s][i] == 1:
            if 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

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

q = int(input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, input().split())
    for friends in F:
        if s in friends and t in friends:
            ans[i] = "yes"

for i in range(q):
    print(ans[i])

Python: from collections import deque
n,m = map(int,input().split())
data = [[] for _ in range(n)]
for _ in range(m):
    a,b = map(int,input().split())
    data[a].append(b)
    data[b].append(a)
visited = [0]*n
kind = [-1]*n
dq = deque([])
for i in range(n):
    if not visited[i]:
        dq.append(i)
        while dq:
            p = dq.pop()
            if not visited[p]:
                kind[p] = i
                visited[p] = 1
                for j in data[p]:
                    dq.append(j)
q = int(input())
for _ in range(q):
    a,b = map(int,input().split())
    print("yes" if kind[a]==kind[b] else "no")

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

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

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

    Q.append(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

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

    C = assignColor(G)
        
    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")
            
if __name__ == "__main__":
    main()