Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	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())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	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 root(s, &group) == root(t, &group) {
			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: def dfs(r, c):
    global c, color, G
    S = [r]
    color[r] = c
    while (0 < len(S)):
        u = S.pop()
        for u in G[u]:
            v = color[u]
            if (v == None):
                color[u] = c
                S.append(u)
 
def assignColor():
    global c, color, G, n
    c = 1
    for i in range(n):
        if (color[i] == None):
            dfs(i, c)
            c += 1
 
n, m = list(map(int, input().split()))
 
G = [None for i in range(n)]
for i in range(m):
    s, t = list(map(int, input().split()))
    G[s].append(t)
    G[t].append(s)
 
color = [False for i in range(n)]
 
assignColor()
 
q = int(input())

for i in range(q):
    s, t = list(map(int, input().split()))
    if (color[s] == color[t]):
        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: from collections import deque

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

G = [[] for i in range(n)]
check = [0 for i in range(n)]


def bfs(s, c):
    q = deque()

    check[s] = 1
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(len(G[u])):
            if check[G[u][v]] == 0:
                check[G[u][v]] = 1
                q.append(G[u][v])

        check[u] = c


def main():
    for i in range(m):
        a, b = map(int, input().split())
        G[a].append(b)
        G[b].append(a)

    cnt = 2
    for i in range(n):
        if check[i] == 0:
            bfs(i, cnt)
            cnt += 1

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


if __name__ == '__main__':
    main()

Python: # AOJ ALDS1_11_D Connected Components
# Python3 2018.6.23 bal4u

# UNION-FIND library
class UnionSet:
	def __init__(self, nmax):
		self.size = [1]*nmax
		self.id = [i for i in range(nmax+1)]
	def root(self, i):
		while i != self.id[i]:
			self.id[i] = self.id[self.id[i]]
			i = self.id[i]
		return i
	def connected(self, p, q): return self.root(p) == self.root(q)
	def unite(self, p, q):
		i, j = self.root(p), self.root(q)
		if i == j: return
		if self.size[i] < self.size[j]:
			self.id[i] = j
			self.size[j] += self.size[i]
		else:
			self.id[j] = i
			self.size[i] += self.size[j]
# UNION-FIND library

n, m = map(int, input().split())
u = UnionSet(n)
for i in range(m):
	s, t = map(int, input().split())
	u.unite(s, t)
q = int(input())
for i in range(q):
	s, t = map(int, input().split())
	print("yes" if u.connected(s, t) else "no")

Python: from collections import deque

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

G = [[] for i in range(n)]
check, dist = [0 for i in range(n)], [-1 for i in range(n)]


def bfs(s, e):
    for i in range(n):
        check[i], dist[i] = 0, -1
    q = deque()

    check[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(len(G[u])):
            if check[G[u][v]] == 0:
                check[G[u][v]], dist[G[u][v]] = 1, dist[u] + 1
                q.append(G[u][v])

        check[u] = 2

    return dist[e]


def main():
    for i in range(m):
        a, b = map(int, input().split())
        G[a].append(b)
        G[b].append(a)

    q = int(input())

    for i in range(q):
        s, t = map(int, input().split())
        k = bfs(s, t)
        if k == -1:
            print("no")
        else:
            print("yes")


if __name__ == '__main__':
    main()

Python: V, E = map(int, input().split())
edge = [[] for _ in range(V)]
for _ in range(E):
    s, t = map(int, input().split())
    edge[s].append(t)
    edge[t].append(s)

group = [-1] * V
cnt = 0
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
for _ in range(int(input())):
    s, t = map(int, input().split())
    print('yes' if group[s] == group[t] else 'no')

Python: def warshall_floyd(w):
    inf = 1E+100
    n = len(w)
    d = w
    for k in range(n):
        d_new = [[inf for _ in range(n)] for _ in range(n)]
        for i in range(n):
            for j in range(n):
                d_new[i][j] = min(d[i][j], d[i][k] + d[k][j])
        d = d_new
    return d


inf = 1E+100
s = [int(x) for x in input().split()]
n = s[0]
m = s[1]
w = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
    w[i][i] = 0
for _ in range(m):
    s = [int(x) for x in input().split()]
    w[s[0]][s[1]] = 1
    w[s[1]][s[0]] = 1

d = warshall_floyd(w)

q = int(input())
for _ in range(q):
    s = [int(x) for x in input().split()]
    u = s[0]
    v = s[1]
    if d[u][v] < inf:
        print("yes")
    else:
        print("no")

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

N, M = tuple(map(int, input().split()))
adj = [[] for n in range(N)]

for _ in range(M):
    inp = tuple(map(int, input().split()))
    adj[inp[0]].append(inp[1])
    adj[inp[1]].append(inp[0])

unsearched = set(range(N))
colors = {n:-1 for n in range(N)}
color = -1

for n in range(N):
    if colors[n] != -1:
        continue
    color += 1
    start = n
    
    que = deque([start])
    while len(que):
        q = que.pop()
        next_ = [i for i in adj[q] if colors[i] == -1]
        que.extendleft(next_)
        colors[q] = color
    
Q = int(input())

for i in range(Q):
    question = tuple(map(int, input().split()))
    if colors[question[0]] == colors[question[1]]:
        print("yes")        
    else:
        print("no")