Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

Python: from collections import deque
n, m = map(int, input().split())
g = [[] for _ in range(n)]
colors = {}
done = set()

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

# print(g)
color = 0

for i in range(n):
    if i in colors:
        continue
    done.clear()
    color += 1
    q = deque()
    q.append(i)

    while q:
        node = q.popleft()
        colors[node] = color
        for next in g[node]:
            if next not in done:
                done.add(next)
                q.append(next)

# print(colors)

Q = int(input())
qs = []
for i in range(Q):
    s, t = map(int, input().split())
    qs.append([s, t])

for i in range(len(qs)):
    if colors[qs[i][0]] == colors[qs[i][1]]:
        print('yes')
    else:
        print('no')

Python: # https://onlinejudge.u-aizu.ac.jp/courses/lesson/1/ALDS1/11/ALDS1_11_D

from collections import defaultdict
import sys
sys.setrecursionlimit(1 << 25)  # 再起上限引き上げ

G = defaultdict(lambda: [])

# load data
N, M = list(map(int, input().split()))
color = {key: None for key in range(N)}  # 色というかグループ #intで表す #Noneは未訪問を示す
# graphをdefault dictで保持することにする。
for _ in range(M):
    s, t = list(map(int, input().split()))
    G[s].append(t)
    G[t].append(s)

# 離接リストをもとに、たどり着ける友達関係をグループ化する #(union findが使える気もするけどここではDFSで領域を探索していく)


def dfs(node: int, co: int):
    # 終了条件 #すでに訪問済みなら訪問しない
    if color[node] is not None:
        return
    # 処理
    color[node] = co
    # 探索
    for n in G[node]:
        dfs(n, co)
    return


# すべてのnodeを開始点としてdfsで領域わけしてみる
c = 1
for node in G.keys():
    dfs(node, c)  # 訪問済みだったら色を塗り替えないというないようはdfs関数内の処理で吸収できる
    c += 1


# 問題に回答していく
Q = int(input())
for _ in range(Q):
    s, t = list(map(int, input().split()))
    if ((color[s] is not None) or (color[t] is not None)) and (color[s] == color[t]):
        # s,tも友達がいて、かつ同じグループだったら
        print('yes')
    else:
        print('no')

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)