Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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: from collections import deque

n, m = map(int, input().split())
g = [set() for _ in range(n)]
gids = [0] * n

while m:
    s, t = map(int, input().split())
    g[s].add(t)
    g[t].add(s)
    m -= 1


def search(s, gid):
    global g, gids
    queue = deque()
    queue.append(s)
    visited = [0] * n
    visited[s] = 1
    gids[s] = gid

    while queue:
        i = queue.popleft()
        for f in g[i]:
            if not visited[f]:
                visited[f] = 1
                gids[f] = gid
                queue.append(f)


gid = 1
for i in range(n):
    if gids[i]:
        continue
    search(i, gid)
    gid += 1

q = int(input())
while q:
    s, t = map(int, input().split())
    gs, gt = gids[s], gids[t]
    print('yes' if gs and gt and gs == gt else 'no')
    q -= 1

Python: #coding: utf-8
n, m = map(int,input().split())
List = [[] for i in range(n)]

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


def search(a,target):
    if a == target:
        print("yes")
        return True
    for member in List[a]:
        if color[member] == "white":
            color[member] = "black"
            return search(member, target)

p = int(input())
for i in range(p):
    color = ["white" for i in range(n)]
    a, target = map(int,input().split())
    color[a] = "black"
    b = search(a, target)
    if b != True:
        print("no")

Python: # coding: UTF-8
from collections import deque
n,m = map(int,input().split())
link = [[] for _ in range(n)]
part = [-1 for _ in range(n)]
for _ in range(m):
  u,v = map(int,input().split())
  link[u].append(v)
  link[v].append(u)

searched = set()
queue = deque()
pindex = 0
for u in range(n):
  if u not in searched:
    queue.append(u)
    pindex += 1
    searched.add(u)
    while queue:
      v = queue.popleft()
      part[v] = pindex
      for w in link[v]:
        if w not in searched:
          part[w] = pindex
          queue.append(w)
          searched.add(w)

q = int(input())
answers = []
for _ in range(q):
  s,t = map(int,input().split())
  if part[s] == part[t]:
    answers.append("yes")
  else:
    answers.append("no")

for answer in answers:
  print(answer)

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(10000000000000)

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

n, m = map(int,input().split())
List = [[] for i in range(n)]
colorOriginal = ["white" for i in range(n)]

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



class DoublyLinkedList():
    def __init__(self, key, l=None, r=None):
        self.l = l
        self.r = r
        self.key = key

def Insert(key,sentinel):
    obj = DoublyLinkedList(key)
    left = sentinel.l
    left.r = obj
    obj.l = left
    obj.r = sentinel
    sentinel.l = obj

def deleteFirst(sentinel):
    right = sentinel.r
    sentinel.r = right.r
    right.r.l = sentinel


def search(a, target):
    sentinel = DoublyLinkedList(None)
    sentinel.l = sentinel
    sentinel.r = sentinel
    
    color = colorOriginal[:]
    Insert(a, sentinel)
    color[a] = "black"
    while sentinel.r.key != None:
        top = sentinel.r.key
        if top == target:
            print("yes")
            break
        for i in List[top]:
            if color[i] == "white":
                Insert(i, sentinel)
                color[i] = "gray"
        color[top] = "black"
        deleteFirst(sentinel)
    else:
        print("no")

ques = int(input())
for i in range(ques):
    a, target = map(int,input().split())
    search(a, target)

Python: from collections import deque

def bfs(graph, start, goal):
    visited = {start: None}
    unvisited = deque()
    unvisited.append(start)
    while unvisited:
        now = unvisited.popleft()
        if now == goal or goal in graph[now]:
            graph[start].add(goal)
            graph[goal].add(start)
            return "yes"
        for next in graph[now]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

n, m = map(int, input().split())
g = [set() for i in range(n)]

for i in range(m):
    a, b = map(int, input().split())
    g[a].add(b)
    g[b].add(a)

q = int(input())

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

Python: import sys
sys.setrecursionlimit(1000000)
n, m = map(int, input().split())
graph = [[] for _ in range(n)]

global visited 
visited = [0 for _ in range(n)]

def input_graph(m):
  for _ in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

def dfs(node_id):
  
  for ad_node in graph[node_id]:
    if visited[ad_node] == 1:
      continue
    visited[ad_node] = 1
    dfs(ad_node)

if __name__ == "__main__":
  input_graph(m)
  q_num = int(input())
  for _ in range(q_num):
    visited = [0 for _ in range(n)]
    s, t = map(int, input().split())
    dfs(s)
    if visited[t]:
      print("yes")
    else:
      print("no")