Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

Python: from collections import deque
n,m = [int(i) for i in input().split()]
F = [{} for i in range(n)]
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
for i in range(q):
    DP = [0]*n
    d = deque()
    s,t = [int(i) for i in input().split()]
    d.append(s)
    flag = False
    while(len(d)>0):
        tmp = d.pop()
        DP[tmp] = 1
        if tmp == t:
            flag = True
            break
        else:
            for i in F[tmp]:
                if not DP[i]:
                    d.append(i)
    if flag:
        print("yes")
    else:
        print("no")

Python: import collections
import sys
sys.setrecursionlimit(100000)

n, m = [int(ele) for ele in input().split()]
graph = [[i] for i in range(n)]
grps = [-1] * n
s = [-1] * (m+1)
t = [-1] * (m+1)

for _ in range(m):
    k, l = [int(ele) for ele in input().split()]
    graph[k].append(l)
    graph[l].append(k)

q = int(input())

s_q = [-1] * (q)
s_t = [-1] * (q)

for i in range(q):
    s_q[i], s_t[i] = [int(ele) for ele in input().split()]


yet = [ele for ele in range(n)]
stack = collections.deque()


def main():
    global graph, grps
    grouping()

    for src, dst in zip(s_q, s_t):
        if grps[src] == grps[dst]:
            print("yes")
        else:
            print("no")


def grouping():
    global yet, stack, grps, tmp
    tmp = 97
    while yet:
        stack.clear()
        stack.appendleft(yet[0])  # 常にmin(yet) = yet[0]
        dfs(stack)
        tmp += 1


def dfs(stack):
    global yet, graph, grps, tmp
    if not stack:
        return

    n = stack[0]
    grps[n] = chr(tmp)
    yet.remove(n)

    for ele in graph[n][1:]:
        if ele in yet:  # 未訪問なら
            stack.appendleft(ele)
            dfs(stack)
    else:
        stack.popleft()


if __name__ == "__main__":
    main()

Python: # -*- coding: utf-8 -*-
from collections import deque


if __name__ == '__main__':

    n, m = [int(s) for s in input().split(" ")]
    M = [[] for j in range(n)]
    for _ in range(m):
        u, v = [int(s) for s in input().split(" ")]
        M[u].append(v)
        M[v].append(u)
        
    color = [0] * n
    def dfs(u, c):  # depth-first search
        color[u] = c
        for v in range(n):
            if v in M[u] and color[v] == 0:
                dfs(v, c)
        color[u] = c
    
    c = 1
    for u in range(n):
        if color[u] == 0:
            dfs(u, c)
            c += 1
    
    l = int(input())
    for _ in range(l):
        p, q = [int(s) for s in input().split(" ")]
        if color[p] == color[q]:
            print("yes")
        else:
            print("no")

Python: n, m = map(int, input().split())
rootList = [-1 for i in range(n)]
 
def getRoot(x):
    if rootList[x] < 0:
        rootList[x] = x
    elif rootList[x] != x:
        rootList[x] = getRoot(rootList[x])
    return rootList[x]
 
 
def setSameRoot(x, y):
    x = getRoot(x)
    y = getRoot(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) != -1 else "no")

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 tan(G,st,fi):
    global flag
    for i in range(len(G)):
        if G[st][i] == 1:
            if i == fi:
                flag = 1
            tan(G,i,fi)
    
n,m = list(map(int, input().split()))
G = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    S = list(map(int, input().split()))
    if S[0]<S[1]:
        G[S[0]][S[1]] = 1
    else:
        G[S[1]][S[0]] = 1

q = int(input())
ans = []
for i in range(q):
    Q = list(map(int, input().split()))
    if Q[0]<Q[1]:
        st = Q[0]
        fi = Q[1]
    else:
        st = Q[1]
        fi = Q[0]
    flag = 0
    tan(G,Q[0],Q[1])
    if flag==1:
        ans.append("yes")
    else:
        ans.append("no")
        
for i in range(q):
    print(ans[i])

Python: import sys

input = sys.stdin.readline

def main():
    n, m = map(int, input().split())
    root = {i:i for i in range(n)}
    
    for i in range(m):
        s, t = map(int, input().split())
        
        r, c = s, 0
        while root[s] != s: s, c = root[s], c+1
        while root[t] != t: root[t], t = s, root[t]
        root[t] = s
        
        if c > 10: 
            while root[r] != s: root[r], r = s, root[r]
        
    for i in range(int(input())):
        s, t = map(int, input().split())
        
        while root[s] != s: s = root[s]
        while root[t] != t: t = root[t]
        
        if s == t: print('yes')
        else: print('no')

if __name__ == '__main__':
    main()

Python: n, m = map(int, input().split())
a = [[False]*n for i in range(n)]
rootList = [-1] * n

for i in range(m):
    s, t = map(int, input().split())
    if rootList[s] < 0:
        rootList[s] = s
    if rootList[t] < 0:
        rootList[t] = t
    if rootList[s] != rootList[t]:
        rootList[s] = min(rootList[s], rootList[t])
        rootList[t] = min(rootList[s], rootList[t])
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print("yes" if rootList[s] == rootList[t] else "no")