Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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

def assignColor():
    C = [-1 for i in range(n)]
    for i in range(n):
        if C[i] == -1:
            C = BFS(i, C)
    return 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

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: 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):
    memo = [0 for i in range(n)]
    data = list(map(int, input().split()))
    memo[data[0]] = 1
    queue = deque(data[0])

    try:
        while len(queue):
            b = queue.popleft()
            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: n, m = map(int, input().split())
rootList = [-1] * n
 
def getRoot(x):
    if rootList[x] < 0:
        rootList[x] = x
        return x
    return getRoot(rootList[x])
 
 
def setSameRoot(x, y):
    x = getRoot(x)
    y = getRoot(y)
    if x != y:
        rootList[x], rootList[y] = min(x, 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: def dfs(r, c):
    global color
    global g
    s = [r]
    color[r] = c
    while len(s) > 0:
        u = s.pop()
        for u in g[u]:
            v = color[u]
            if v is None:
                color[u] = c
                s.append(u)
                
def assigncolor():
    global g
    global color
    global n
    c = 1
    for i in range(n):
        if color[i] == None:
            dfs(i, c)
            c += 1
            
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)
    
color = [None for i in range(n)]

assigncolor()

q = int(input())
for i in range(q):
    if color[s] == color[t]:
        print("yes")
    else:
        print("no")

Python: import sys
def s():
 e=sys.stdin.readline
 n,m=map(int,e().split())
 P=[[]for _ in[0]*n]
 C=[0]*n
 k=0
 for _ in[0]*m:
  s,t=map(int,e().split());P[s]+=[t];P[t]+=[s]
 for i in range(n):
  k+=1
  if C[i]<1:
   s=[i];C[i]=k
   while s:
    u=s.pop()
    for v in P[u]:
     if C[v]<1:C[v]=k;s+=[v]
 for _ in[0]*int(e()):
  a,b=map(int,e().split())
  print(['no','yes'][C[a]==C[b]])
s()

Python: n_people, n_relationship = list(map(int, input().split()))
adj_list = [[] for _ in range(n_people)]
for i in range(n_relationship):
  p, target = list(map(int, input().split()))
  adj_list[p].append(target)
  adj_list[target].append(p)


group = [-1] * n_people
cnt = 0
for i in range(n_people):
  if group[i] == -1:
    group[i] = cnt
    stack = [i]
    while stack:
      at = stack.pop()
      for to in adj_list[at]:
        if group[to] == -1:
          group[to] = cnt
          stack.append(to)

    cnt += 1

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

Python: 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)                #sのお隣さんリストにtを
  edge[t].append(s)                #tのお隣さんリストにsを

group = [-1]*v                     #グループというリストを作る（こいつの値であとで友達か照合）
cnt = 0                            #グループ番号


#隣接リストに入っている同じエッジをスタックに入れてグループ化する
for i in range(v):
  if group[i] == -1:
    group[i] = cnt                 #カウントの値を入れる
    stack = [i]                    #エッジ番号を入れる
    while stack:
      v = stack.pop()              #エッジを代入　ー①
      for c in edge[v]:            #①のエッジのお隣さんリストを辿っていく
        if group[c] == -1:         #お隣さんのグループが初期化状態なら
          group[c] = cnt          #グループリストにカウントを入れる
          stack.append(c)          #スタックにお隣さんを入れる
    cnt += 1                       #お隣さんの訪問が終わったらグループ番号を変える

#クエリのコード
for _ in range(int(input())):
  s, t = map(int, input().split())
  print('yes' if group[s] == group[t] else 'no')

Python: 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)

group = {}
c = 0
for i in range(n):
    if not i in group:
        que = [i]
        group[i] = c
        while len(que) > 0:
            u = que[0]
            for j in G[u]:
                if not j in group:
                    group[j] = que.append(j)
            del que[0]
        c += 1

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

Python: import sys
input = sys.stdin.readline

from collections import deque, defaultdict
from copy import deepcopy

MAX = 100000
NIL = -1

#グラフGはdictで管理する
G = defaultdict(lambda:deque())
color = [NIL]*MAX

#頂点r,探索ごとに異なる数cによって深さ優先探索をする関数
#つながっている部分は全て色が同じになる
def dfs(r, c):
    S = deque()
    S.append(r) 
    color[r] = c
    while S: #Sが存在する間以下の処理を実行
        u = S.popleft()
        for v in G[u]:#Gのvalueはqueueとなっている
            if color[v] == NIL:#色がない場合のみ以下の操作を行う
                color[v] = c
                S.append(v) #深さを深くして次の頂点を探す準備をする

def make_color():
    id = 1
    for u in range(n):
        if color[u] == NIL:
            dfs(u, id) #色を塗る
            id += 1 #色を変える
        
    
    
n, m = map(int, input().split())

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

make_color()

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