Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	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())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	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 group[s] == group[t] {
			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: n = list(map(int,input().split()))
cn = [i for i in range(n[0])]
cnt = [[0]]*n[0]
for i in range(1,n[0]):
    cnt[i] = [i]
x = [1]*n[0]

for i in range(n[1]):
    a = list(map(int,input().split()))
    if cn[a[0]] != cn[a[1]]:
        if x[a[0]] > x[a[1]]: 
            tmp = cn[a[1]]
            tmp2 = cn[a[0]]
        else: 
            tmp = cn[a[0]]
            tmp2 = cn[a[1]]
        for j in cnt[tmp]:
            cn[j] = cn[tmp2]
        x[tmp2] += len(cnt[tmp])
        cnt[tmp2].extend(cnt[tmp])
        cnt[tmp] = [-1]

q = int(input())
for i in range(q):
    a = list(map(int,input().split()))
    print("yes" if cn[a[1]] == cn[a[0]] else "no")

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

import sys
sys.setrecursionlimit(1000000000)

def dfs(u, group):
    isVisited[u] = True
    CC[u] = group
    for v in adj[u]:
        if not isVisited[v]:
            dfs(v, group)

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: import sys
sys.setrecursionlimit(200000)


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: 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(n):
            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 -*-
import copy

level = False
def debug(v):
    if level:
        print(v)

NOT_FOUND = 0
ENQUEUED = 1
FOUND = 2

def dfs(graph, start, end):
    stack = [start]
    status = [NOT_FOUND] * len(graph)

    def loop(stack, status, graph, start, end):
        debug("-------------------")
        debug(stack)
        debug(graph)
        if len(stack) == 0:
            debug(stack)
            return False

        v = stack.pop()
        status[v] = FOUND
        if v == end:
            return True

        edges = graph[v]
        if len(edges) != 0:
            stack.append(v)
            e = edges.pop(0)
            if e == end:
                return True
            if status[e] == NOT_FOUND:
                stack.append(e)
                status[e] = ENQUEUED

        return loop(stack, status, graph, start, end)

    return loop(stack, status, copy.deepcopy(graph), start, end)

def bfs(graph, start, target):
    debug(graph)
    if start == target:
        return True
    
    queue = []
    status = [NOT_FOUND] * len(graph)
    queue.append(start)

    while len(queue) != 0:
        node = queue.pop(0)
        status[node] = FOUND
        edges = graph[node]
        for e in edges:
            if e == target:
                return True
            if status[e] == NOT_FOUND:
                status[e] = ENQUEUED
                queue.append(e)

    return False

if __name__ == "__main__":
    n, m = [int(x) for x in input().split()]

    g = [[] for i in range(n)]
    for i in range(m):
        u, e = [int(x) for x in input().split()]
        g[u].append(e)
        g[e].append(u)

    n = int(input())
    for i in range(n):
        s, d = [int(x) for x in input().split()]
        if dfs(g, s, d):
            print("yes")
        else:
            print("no")

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

def assignColor():
    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 = [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():
    
    for i in range(m):
        s, t = list(map(int, input().split()))
        G[s].append(t)
        G[t].append(s)

    C = 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")
            
if __name__ == "__main__":
    n, m = list(map(int, input().split()))
    G = [[] for i in range(n)]

    main()

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
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 root(s) == root(t) else "no")

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
findSet = lambda x, y: 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")