Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

Python: import queue

N, M = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(M):
    s, t = map(int, input().split())
    G[s].append(t)
    G[t].append(s)

group = [0] * N
group_id = 1
que = queue.Queue()

for i in range(N):
    if group[i] != 0:
        continue
    group[i] = group_id
    que.put(G[i])
    while not que.empty():
        q = que.get()
        for j in q:
            if group[j] == 0:
                que.put(G[j])
                group[j] = group_id
    group_id += 1

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

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:
10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3

output:
yes
yes
no

"""

import sys


def assign_color():
    current_color = 1
    for v in range(v_num):
        if color[v] == -1:
            graph_dfs(v, current_color)
            current_color += 1
    return None


def graph_dfs(current, current_color):
    stack = list()
    stack.append(current)
    color[current] = current_color

    while stack:
        current = stack.pop()
        for v in adj_table[current]:
            if color[v] == -1:
                color[v] = current_color
                stack.append(v)
    return None


def solve():
    for edge in edges:
        source, target = map(int, edge)
        adj_table[source].append(target)
        adj_table[target].append(source)

    assign_color()

    for question in questions:
        q_source, q_target = map(int, question)
        if color[q_source] == color[q_target]:
            print("yes")
        else:
            print("no")
            
    return color


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    v_num, r_num = map(int, _input[0].split())

    edges = map(lambda x: x.split(), _input[1:r_num + 1])

    q_num = int(_input[r_num + 1])
    questions = map(lambda x: x.split(), _input[r_num + 2:])

    adj_table = tuple([] for _ in range(v_num))
    color = [-1] * v_num
    ans = solve()

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

Python: def BFS(s = 0, group_num = 0):
    Q.pop(0)
    color[s] = 2
    for j in range(len(A[s])):
        if color[A[s][j]] == 0:
            Q.append(A[s][j])
            color[A[s][j]] = 1
            affil[A[s][j]] = group_num
    if len(Q) != 0:
        BFS(Q[0], group_num)

entry = map(int, raw_input())
n = entry[0]
m = entry[1]

A = [0] * n
for i in range(n):
    A[i] = list()
for i in range(m):
    entry = map(int, raw_input())
    s = entry[0]
    t = entry[1]
    A[s].append(t)
    A[t].append(s)

color = [0] * n
affil = [0] * n
group_num = -1
for i in range(n):
    if color[i] == 0:
        Q = [i]
        group_num = group_num + 1
        affil[i] = group_num
        BFS(i, group_num)

q = int(raw_input())
for i in range(q):
    entry = map(int, raw_input())
    if affil[entry[0]] == affil[entry[1]]:
        print("yes")
    else:
        print("no")

Python: def bfs(graph, start, goal, connection):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        if now == goal or (1000000*now + goal) in connection:
            connection[1000000*start + goal] = 1
            return "yes"
        unvisited = unvisited[1:]
        for next in graph[now][1:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

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

for i in range(m):
    inp = list(map(int, input().split()))
    g[inp[0]].append(inp[1])
    g[inp[1]].append(inp[0])

q = int(input())

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

Python: def DFS(s):
    flag[s] = 1
    group[s] = j
    for i in range(n):
        if G[s][i] == 1 and 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

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

Python: def DFS(s):
    flag[s] = 1
    F[group] += 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 friends in F:
        if s in friends and t in friends:
            ans[i] = "yes"

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

Python: class QuickUnion:
    def __init__(self, n):
        self.conn = [i for i in range(n)]
        self.size = [1 for _ in range(n)]

    def union(self, a, b):
        ra = self._root(a)
        rb = self._root(b)

        if ra == rb:
            return

        if self.size[ra] > self.size[rb]:
            self.conn[rb] = ra
            self.size[ra] += self.size[rb]
        else:
            self.conn[ra] = rb
            self.size[rb] += self.size[ra]

    def connected(self, a, b):
        ra = self._root(a)
        rb = self._root(b)

        return ra == rb

    def _root(self, a):
        p = self.conn[a]
        while p != a:
            pp = self.conn[p]
            self.conn[a] = pp
            a, p = p, pp

        return a


def run():
    n, m = [int(i) for i in input().split()]
    u = QuickUnion(n)

    for _ in range(m):
        x, y = [int(i) for i in input().split()]
        u.union(x, y)

    q = int(input())

    for _ in range(q):
        x, y = [int(i) for i in input().split()]
        if u.connected(x, y):
            print('yes')
        else:
            print('no')


if __name__ == '__main__':
    run()