Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

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")