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: 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()

Python: n, m = map(int, input().split())

friend = [ [] for _ in range(n) ]

for _ in range(m):
    s, t = map(int, input().split())
    friend[s].append(t)
    friend[t].append(s)

flag = [0] * n
group = 0

# def dfs(u):
#    for v in friend[u]:
#        if flag[v] == 0:
#            flag[v] = group
#            dfs(v)

for i in range(n):
    if flag[i] == 0:
        group += 1
        flag[i] = group
        # dfs(i)
        
        stack = [i]
        
        while stack:
            u = stack.pop()
            for v in friend[u]:
                if flag[v] == 0:
                    flag[v] = group
                    stack.append(v)

q = int(input())

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

Python: import sys
from collections import deque
input = sys.stdin.readline

V, E = map(int, input().split())
G = {i: deque([]) for i in range(V)}
for _ in range(E):
    v1, v2 = map(int, input().split())
    G[v1].append(v2)
    G[v2].append(v1)


seen = [0]*(V)
stack = []
color = {v: -1 for v in range(V)}

def dfs(j, col):
    global time
    if seen[j]:
        return
    seen[j] = 1
    stack.append(j)
    color[j] = col
    while stack:
        s = stack[-1]
        if not G[s]:
            stack.pop()
            continue
        g_NO  = G[s].popleft()
        if seen[g_NO]:
            continue
        color[g_NO] = col
        seen[g_NO] = 1
        stack.append(g_NO)
    
c = 0
for start in range(V):
    if seen[start]:
        continue
    dfs(start, c)
    c += 1


q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    if color[s] != -1 and color[t] != -1 and color[s] == color[t]:
        print("yes")
    else:
        print("no")

Python: n,m = map(int,input().split())
Rel = [tuple(map(int,input().split())) for _ in range(m)]
q = int(input())
Que = [tuple(map(int,input().split())) for _ in range(q)]

class UF:
    def __init__(self):
        self.roots = [i for i in range(n)]
        self.rank = [1 for _ in range(n)]

    def same(self,a,b):
        if self.find(a) == self.find(b):
            return True
        else:
            return False

    def find(self,a):
        if self.roots[a] == a:
            return a
        else:
            self.roots[a] = self.find(self.roots[a])
            return self.roots[a]

    def union(self,a,b):
        a,b = self.find(a),self.find(b)
        if a != b:
            if self.rank[a] >= self.rank[b]:
                self.roots[b] = a
                if self.rank[a] == self.rank[b]:
                    self.rank[b] += 1
            else:
                self.roots[a] = b

uf = UF()

for i in range(m):
    uf.union(Rel[i][0],Rel[i][1])

for i in range(q):
    if uf.same(Que[i][0],Que[i][1]):
        print("yes")
    else:
        print("no")