Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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

Python: # Graph I - Connected Components
import array
from collections import deque
def groupify(length, adj_list):
    group = array.array('l', (-1 for _ in range(length)))
    g_id = 0
    dq = deque()
    for idx in range(length):
        if group[idx] == -1:
            group[idx] = g_id
            dq.append(idx)
            while len(dq):
                v = dq.popleft()
                for t in adj_list[v]:
                    if group[t] == -1:
                        group[t] = g_id
                        dq.append(t)
            g_id += 1
    return group

n,m = map(int, input().split())
adj_list = [set() for _ in range(n)]
for _ in range(m):
    i,j = map(int, input().split())
    adj_list[i].add(j)
    adj_list[j].add(i)
group = groupify(n, adj_list)

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