Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t 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() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		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()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def assign_color():
    _color = 1
    for m in range(vertices_num):
        if vertices_status_list[m] == -1:
            graph_dfs(m, _color)
            _color += 1
    return None


def graph_dfs(vertex, color):
    vertices_stack = list()
    vertices_stack.append(vertex)
    vertices_status_list[vertex] = color

    while vertices_stack:
        current_vertex = vertices_stack[-1]
        vertices_stack.pop()
        for v in adj_list[current_vertex]:
            if vertices_status_list[v] == -1:
                vertices_status_list[v] = color
                vertices_stack.append(v)
    return None


def solve():
    for relation in relation_info:
        key, value = map(int, relation.split())
        adj_list[key].append(value)
        adj_list[value].append(key)

    assign_color()

    # print(adj_list)
    for question in question_list:
        start, target = map(int, question.split())
        if vertices_status_list[start] == vertices_status_list[target]:
            print("yes")
        else:
            print("no")
    return vertices_status_list


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num, relation_num = map(int, _input[0].split())

    relation_info = _input[1:relation_num + 1]

    question_num = int(_input[relation_num + 1])
    question_list = _input[relation_num + 2:]

    adj_list = tuple([[] for _ in range(vertices_num)])
    vertices_status_list = [-1] * vertices_num
    ans = solve()

Python: from sys import stdin
n, m = map(int, stdin.readline().split())
G = [-1] * n
def root(x):
    if G[x] < 0: return x
    G[x] = root(G[x])
    return G[x]
def uniteSet(x, y):
    x = root(x)
    y = root(y)
    if x == y : return
    if G[x] > G[y] : x, y = y, x
    G[y] = x
def findSet(x, y):
    return root(x) == root(y)
for i in range(0, m):
    s, t = map(int, stdin.readline().split())
    uniteSet(s, t)
q = int(stdin.readline())
for i in range(0, q):
    s, t = map(int, stdin.readline().split())
    print("yes" if findSet(s, t) else "no")

Python: # -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 10:14:13 2018
ALDS1_11_D
@author: maezawa
"""
import sys
sys.setrecursionlimit(100000)

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: import sys
sys.setrecursionlimit(10**6)


def dfs(v, cnt):
    group[v] = cnt
    for c in edge[v]:
        if group[c] == -1:
            dfs(c, cnt)

V, E = map(int, input().split())
edge = [[] for _ in range(V)]
for _ in range(E):
    s, t = map(int, input().split())
    edge[s].append(t)
    edge[t].append(s)
group = [-1] * V
c = 0
for i in range(V):
    if group[i] == -1:
        dfs(i, c)
        c += 1

Q = int(input())
for _ in range(Q):
    s, t = map(int, input().split())
    print('yes' if group[s] == group[t] else 'no')

Python: #coding:utf-8
import sys
sys.setrecursionlimit(100000)
n, r = map(int,input().split())
Q = []
M = [[] for j in range(n)]
for i in range(r):
    i,j = map(int,input().split())
    M[i].append(j)
    M[j].append(i)


def search(person, target):
    global yes
    if yes == 1:
        return
    data = M[person]
    for per in data:
        if per ==  target:
            yes = 1
            return
        if color[per] == "white":
            color[per] = "gray"
            search(per, target)
    color[person] = "black"

num = int(input())
for i in range(num):
    yes = 0
    color = ["white" for j in range(n)]
    person, target = map(int,input().split())
    search(person, target)
    if yes == 0:
        print("no")
    else:
        print("yes")

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

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

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)

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
# sys.stdin = open('input.txt')

def dfs(r, c):
    S = []
    S.append(r)
    color[r] = c
    while len(S) > 0:
        u = S.pop()
        for v in G[u]:
            if color[v] is None:
                color[v] = c
                S.append(v)

def assignColors():
    colorId = 0
    for i in range(n):
        if color[i] is None:
            dfs(i, colorId)
            colorId += 1

n, m  = map(int, input().split())
G = [[] for _ in range(n)]
color = [None] * n

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

assignColors()

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