Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

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

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)

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

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


def dfs(u, group):
    path = []
    path.append(u)
    isVisited[u] = True
    CC[u] = group
    while path:
        u = path[-1]
        if adj[u]:
            v = adj[u].pop(0)
            if not isVisited[v]:
                isVisited[v] = True
                CC[v] = group
                path.append(v)
        else:
            path.pop()

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: def connected(G,s,t):
    checked = [False]*len(G)
    def main(G,s,t):
        if t in G[s]: return True
        checked[s] = True
        for f in G[s]:
            if checked[f]: continue
            if main(G,f,t): return True
        return False
    return main(G,s,t)


if __name__=='__main__':
    n,m = list(map(int,input().split()))
    G = [[] for _ in range(n)]
    for _ in range(m):
        s,t = list(map(int,input().split()))
        G[s].append(t)
        G[t].append(s)
    q = int(input())
    for _ in range(q):
        s,t = list(map(int,input().split()))
        print('yes' if connected(G,s,t) else 'no')

Python: n, m = map(int, input().split())
relations = [[] for _ in range(n)]

for i in range(m):
    s, t = map(int, input().split())
    if s > t:
        s, t = t, s
    relations[s] += [t]
friend_id = [None] * n
id = 0
for s, t in enumerate(relations):
    if t:
        queue = [s]
        while len(queue):
            k = queue.pop()
            friend_id[k] = id
            queue += relations[k]
            relations[k] = []
    id += 1

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

Python: from collections import deque

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

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


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

    while queue:
        st, i = queue.popleft()
        if visited[not st][i]:
            return True
        for f in g[i]:
            if not visited[st][f]:
                visited[st][f] = 1
                queue.append((st, f))
    return False


q = int(input())
while q:
    s, t = map(int, input().split())
    print('yes' if find(s, t) else 'no')
    q -= 1

Python: V, E = map(int, input().split())
Edge = [[] for _ in range(V)]

for _ in range(E): #??£????????????
    u, v = map(int, input().split())
    Edge[u].append(v)
    Edge[v].append(u)
    
Group = [-1] * V #Group???????????????????????°?????????????±????????????????????????°?????????????????°??????????????????????????????
Cnt = 0 # Group???????????????????±????????????????

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

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

Python: #!/usr/bin/python3
n, m = map(int, input().split())
ALL = [0] * n
Graph = []
for i in range(m):
    s, t = map(int, input().split())
    if ALL[s] == 0 and ALL[t] == 0:
        Graph.append(set([s, t]))
    elif ALL[s] == 0:
        for g in Graph:
            if t in g:
                g.add(s)
                break
    elif ALL[t] == 0:
        for g in Graph:
            if s in g:
                g.add(t)
                break
    else:
        sg = None
        tg = None
        for g in Graph:
            if s in g:
                sg = g
            if t in g:
                tg = g

        if sg is not tg:
            sg.update(tg)
            Graph.remove(tg)

    ALL[s] = 1
    ALL[t] = 1
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    for g in Graph:
        if s in g and t in g:
            print('yes')
            break
    else:
        print('no')