Go: package main

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

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

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if root(s, &group) == root(t, &group) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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, raw_input().split())
G = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    s, t = map(int, raw_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(raw_input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, raw_input().split())
    if group[s] == group[t]:
        ans[i] = "yes"
for i in range(q):
    print(ans[i])

print(group)

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

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

for i in range(n):
    for j in range(n):
        for k in range(n):
            if G[j][k] == 1:
                for l in range( n):
                    if G[k][l] == 1:
                        G[j][l] = 1
                        G[l][j] = 1

q = int(raw_input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, raw_input().split())
    if G[s][t] == 1:
        ans[i] = "yes"
for i in range(q):
    print(ans[i])

Python: import sys
sys.setrecursionlimit(1000000)


def depth_first_search(node, friend_list, color_list, color):
    color_list[node] = color
    # print(color_list)
    for friend in friend_list[node]:
        if color_list[friend] == -1:
            color_list = depth_first_search(friend, friend_list,\
                 color_list, color)
    return color_list
        

# f =  open('alds1_11_d-input3.txt', 'r')
# n_user, n_relation = list(map(int, f.readline().split()))
n_user, n_relation = list(map(int, input().split()))

friend_list = [[] for _ in range(n_user)]
for _ in range(n_relation):
    # user, friend = list(map(int, f.readline().split()))
    user, friend = list(map(int, input().split()))
    friend_list[user].append(friend)
    friend_list[friend].append(user)


color_list = [-1 for _ in range(n_user)]
color = 0
for user in range(n_user):
    if color_list[user] == -1:
        color_list = depth_first_search(user, friend_list,\
                color_list, color)
        color += 1

# n_question = int(f.readline())
n_question = int(input())
for _ in range(n_question):
    # user, friend = list(map(int, f.readline().split()))
    user, friend = list(map(int, input().split()))
    if color_list[user] == color_list[friend]:
        print('yes')
    else:
        print('no')
# f.close()

Python: # -*- coding: utf-8 -*-
"""
Created on Sat Jul 14 10:14:13 2018
ALDS1_11_D
@author: maezawa
"""

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
    stack.append(u)
    while stack:
        current = stack[-1]
        v[current] = color
        #print(u, current, stack)
        flag = 0
        for k in adj[current]:
            if v[k] > 0:
                continue
            stack.append(k)
            flag = 1
        if flag == 0:
            stack.pop()
    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 DFS(s):
    flag[s] = 1
    group[j].append(s)
    for i in range(n):
        if i in G[s] and flag[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)

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

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

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)

isVisited = [False] * n

CC = [None] * n


def dfs(u, group):
    path = []
    path.append(u)
    isVisited[u] = True
    CC[u] = group
    while path:
        u = path[-1]
        if adj[u]:
            v = adj[u].pop(0)
            if not isVisited[v]:
                isVisited[v] = True
                CC[v] = group
                path.append(v)
        else:
            path.pop()

def makeCC(group):
    for i in range(n):
        if not isVisited[i]:
            dfs(i, group)
            group += 1

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


makeCC(0)


q = int(input())

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

Python: def connected(G,s,t):
    checked = [False]*len(G)
    def main(G,s,t):
        if t in G[s]: return True
        checked[s] = True
        for f in G[s]:
            if checked[f]: continue
            if main(G,f,t): return True
        return False
    return main(G,s,t)


if __name__=='__main__':
    n,m = list(map(int,input().split()))
    G = [[] for _ in range(n)]
    for _ in range(m):
        s,t = list(map(int,input().split()))
        G[s].append(t)
        G[t].append(s)
    q = int(input())
    for _ in range(q):
        s,t = list(map(int,input().split()))
        print('yes' if connected(G,s,t) else 'no')

Python: n, m = map(int, input().split())
relations = [[] for _ in range(n)]

for i in range(m):
    s, t = map(int, input().split())
    if s > t:
        s, t = t, s
    relations[s] += [t]
friend_id = [None] * n
id = 0
for s, t in enumerate(relations):
    if t:
        queue = [s]
        while len(queue):
            k = queue.pop()
            friend_id[k] = id
            queue += relations[k]
            relations[k] = []
    id += 1

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