Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; 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)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

Python: n, m = map(int, raw_input().split())
G = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    s, t = map(int, raw_input().split())
    G[s][t] = 1
    G[t][s] = 1

for i in range(n+1):
    for j in range(n):
        for k in range(n):
            if G[j][k] == 1:
                for l in range(k+1, n):
                    if G[k][l] == 1:
                        G[j][l] = 1

q = int(raw_input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, raw_input().split())
    if G[s][t] == 1:
        ans[i] = "yes"
for i in range(q):
    print(ans[i])

Python: import sys
from collections import deque

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def paint_col(adj, cols, i, col):
    nxt = deque([i])

    while nxt:
        u = nxt.popleft()
        cols[u] = col
        for child in adj[u]:
            if cols[child] == 0:
                nxt.append(child)

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

    cols = [0] * n
    col = 1

    for i in range(n):
        if cols[i] == 0:
            paint_col(adj, cols, i, col)
            col += 1

    q = int(input())

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

        if cols[s] == cols[t]:
            ans = 'yes'
        else:
            ans = 'no'

        print(ans)

if __name__ == '__main__':
    solve()

Python: import sys
readline = sys.stdin.readline
def MAIN():
    n, m = map(int, input().split())
    li = [i for i in range(n)]
    def f(a):
        if li[a] == a:
            return a
        li[a] = f(li[a])
        return li[a]
    def f2(a, b):
        if li[a] == a:
            li[a] = b
            return
        f2(li[a], b)
        li[a] = b
    for _ in range(m):
        x, y = map(int, readline().split())
        if f(x) > f(y):
            f2(x, li[y])
        elif f(x) < f(y):
            f2(y, li[x])
    for _ in range(int(input())):
        x, y = map(int, readline().split())
        print("yes" if f(a) == f(b) else "no")
MAIN()

Python: def bfs(s, t):
  q = [s]
  while len(q) > 0:
    v = q.pop(0)
    if v == t: return True
    if len(G[v]) > 0:
      q.extend(sorted(G[v]))
      G[v] = []
  return False

u, n = map(int, input().split())
G = [[] for _ in range(u)]
for _ in range(n):
  a, b = map(int, input().split())
  if b not in G[a]: G[a].append(b)
  if a not in G[b]: G[b].append(a)

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

Python: from collections import deque

def dfs(r: int, c: int) -> None:
	S = deque()
	S.append(r)
	color[r] = c
	while S:
		u = S.pop()
		for v in G[u]:
			if color[v] == None:
				color[v] = c
				S.append(v)

def assignColor():
	id: int = 1
	for u in range(n):
		if color[u] == None:
			dfs(u, id)
			id += 1


if __name__ == "__main__":
	n, m = map(int, input().split())
	G = [[] for _ in range(n)]
	for _ in range(m):
		u, v = map(int, input().split())
		G[u].append(v)
		G[v].append(u)
	color = [None for _ in range(n)]
	assignColor()
	q = int(input())
	for _ in range(q):
		u, v = map(int, input().split())
		if color[u] == color[v]:
			print("yes")
		else:
			print("no")

Python: import sys
import copy
from collections import deque
sys.setrecursionlimit(20000)
N,relation=input().split()
N,relation=int(N),int(relation)
g = [set() for i in range(N)]

def BFS(a,b):
    bfs = deque([a])
    grp={a:None}
    grp2={a}
    while bfs:
        i=bfs.popleft()
        for j in g[i]:
            if j not in grp2:
                grp[j]=a
                grp2.add(j)
                bfs.append(j)
    return grp,grp2


for i in range(relation):
    kari=[]
    j,k=list(map(int,input().split()))
    g[j].add(k)
    g[k].add(j)
group=[]
tansaku=int(input())
for i in range(tansaku):
    a,b=list(map(int,input().split()))
    for k in range(len(group)):
        if a in group[k] and b in group[k]:
            print("yes")
            break
        elif a in group[k]:
            print("no")
            break
    else:
        grp,grp2=(BFS(a,b))
        grp=set(grp.keys())
        if b in grp:
            print("yes")
        else:print("no")
        group.append(grp)

Python: import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def paint_col(adj, cols, i, col):
    nxt = [i]

    while nxt:
        u = nxt[-1]
        try:
            v = next(adj[u])
            if cols[v] == 0:
                cols[v] = col
                nxt.append(v)
        except StopIteration:
            nxt.pop()
            cols[u] = col

def solve():
    n, m = map(int, sys.stdin.readline().split())
    adj = [[] for i in range(n)]

    for i in range(m):
        s, t = map(int, sys.stdin.readline().split())
        adj[s].append(t)
        adj[t].append(s)

    cols = [0] * n
    col = 1
    adj = list(map(iter, adj))

    for i in range(n):
        if cols[i] == 0:
            paint_col(adj, cols, i, col)
            col += 1

    debug(cols, locals())

    q = int(sys.stdin.readline())

    for i in range(q):
        s, t = map(int, sys.stdin.readline().split())

        if cols[s] == cols[t]:
            ans = 'yes'
        else:
            ans = 'no'

        print(ans)

if __name__ == '__main__':
    solve()

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)
    if x != 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")