Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func newGraph(nNodes int) *Graph {
	g := Graph{}
	g.nodes = make([]*GraphNode, nNodes)
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode] = &GraphNode{}
		g.nodes[iNode].key = GraphNodeKey(iNode)
	}
	return &g
}

var infty = 1000000000

type GraphNodeKey int

// GraphNode is a node for undirected graph
type GraphNode struct {
	key       GraphNodeKey
	adjacents []*GraphNode
	visited   bool
	d, f      int
	parent    *GraphNode
	cc        int
}

func (g *Graph) setAdjacent(l, r GraphNodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, g.nodes[r])
}

// Graph is a undirected graph
type Graph struct {
	nodes []*GraphNode
}

func (g *Graph) initialize() {
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode].visited = false
		g.nodes[iNode].d = infty
		g.nodes[iNode].f = infty
		g.nodes[iNode].parent = nil
		g.nodes[iNode].cc = -1 // 22.3-12
	}
}

type QueueEntry struct {
	node *GraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *GraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

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

var time = 0

func fillDistancesByDepthFirstSearch(g *Graph) {
	g.initialize()
	time = 0
	cc := 0

	for _, u := range g.nodes {
		if !u.visited {
			dfsVisit(g, u, cc)
			cc++
		}
	}
}

func dfsVisit(g *Graph, u *GraphNode, cc int) {
	time++
	u.d = time
	u.cc = cc

	u.visited = true
	for _, v := range u.adjacents {
		if !v.visited {
			v.parent = u
			dfsVisit(g, v, cc)
		}
	}
	time++
	u.f = time
}

func main() {
	scanner.Split(bufio.ScanWords)

	nUsers := nextInt()
	nEdges := nextInt()
	graph := newGraph(nUsers)
	for iEdge := 0; iEdge < nEdges; iEdge++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())
		graph.setAdjacent(l, r)
		graph.setAdjacent(r, l)
	}

	fillDistancesByDepthFirstSearch(graph)

	nQueries := nextInt()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())

		// fmt.Println(graph.nodes[l].d, graph.nodes[r].d, graph.nodes[r].f, graph.nodes[l].f)
		if graph.nodes[l].cc == graph.nodes[r].cc {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

Python: #!/usr/bin/env python3

NIL: int = -1


class DFS():
    def __init__(self, n, G, debug=False):
        self.n = n
        self.G = G
        self.color = [NIL] * n

        self.debug = debug
        self._debug_print_G()
        self._debug_print_color()

    def assign_color(self):
        id = 1                  # color の id
        for u in range(self.n):
            if self.color[u] == NIL:
                self._dfs(u, id)
                id += 1
        self._debug_print_color()

    def can_reach(self, s, t) -> ():
        """boolを返すっぽいけど、yes / no を　print するメソッド"""
        if self.color[s] == self.color[t]:
            print('yes')
        else:
            print('no')

    def _dfs(self, r: int, c: int):
        S = []                  # list をスタックとして使う
        S.append(r)
        self.color[r] = c
        while len(S) > 0:
            u = S.pop()
            for v in self.G[u]:
                if self.color[v] == NIL:
                    self.color[v] = c
                    S.append(v)

    def _debug_print_G(self):
        if not self.debug:
           return
        for i in range(self.n):
            print(f'{i}: {self.G[i]}')

    def _debug_print_color(self):
        if not self.debug:
           return
        print(f'color: {self.color}')


def main():
    n, m = [int(char) for char in input().split()]

    G = [[] for _ in range(n)]  # n要素の配列の配列
    for _ in range(m):
        s, t = [int(char) for char in input().split()]
        G[s].append(t)
        G[t].append(s)
    dfs = DFS(n, G)
    dfs.assign_color()

    q = int(input())
    for _ in range(q):
        s, t = [int(char) for char in input().split()]
        dfs.can_reach(s, t)


if __name__ == '__main__':
    main()

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())
for _ in range(q):
  s,t = map(int,input().split())
  if part[s] == part[t]:
    print("Yes")
  else:
    print("No")

Python: import sys
sys.setrecursionlimit(15000)
n,m = map(int,sys.stdin.readline().split())
g = list(range(n))
def find(x):
  while g[x] != x:
    x = g[x]
    g[x] = g[g[x]]
  return x
def union(s,t):
  sp = find(s)
  tp = find(t)
  if sp == tp:
    pass
  elif sp < tp:
    g[sp] = tp
  else:
    g[tp] = sp
for i in range(m):
  s,t = map(int,sys.stdin.readline().split())
  union(s,t)

q = int(sys.stdin.readline())
for i in range(q):
  s,t = map(int,sys.stdin.readline().split())
  if find(s) == find(t):
    print("yes")
  else:
    print("no")

Python: def s():
 n,m=map(int,input().split())
 P=[[]for _ in[0]*n]
 C=[0]*n
 k=0
 for _ in[0]*m:
  s,t=map(int,input().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(input()):
  a,b=map(int,input().split())
  print(['no','yes'][C[a]==C[b]])
if'__main__'==__name__:s()

Python: from collections import deque

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

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

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())
    for j in range(len(group)):
        if s in group[j] and t in group[j]:
            print("yes")
            break
        elif s in group[j]:
            print("no")
            break
    else:
        group_tmp = set(bfs(g, s).keys())
        if t in group_tmp:
            print("yes")
        else:
            print("no")

Python: def DFS(s):
    flag[s] = 1
    F[group].append(s)
    for i in range(n):
        if G[s][i] == 1:
            if flag[i] == 0:
                DFS(i)
 

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

F = []
flag = [0 for i in range(n)]
group = 0
for i in range(n):
    if flag[i] == 0:
        F.append([])
        DFS(i)
        group += 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(F)):
        if s in F[j] and t in F[j]:
            ans[i] = "yes"

for i in range(q):
    print(ans[i])