Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

Python: #!/usr/bin/python3
n, m = map(int, input().split())

e = [None] * n

for i in range(m):
    s, t = map(int, input().split())
    if e[s] == None and e[t] == None:
        a = {s, t}
        e[s] = e[t] = a
    elif e[s] == None:
        e[t].add(s)
        e[s] = e[t]
    elif e[t] == None:
        e[s].add(t)
        e[t] = e[s]
    else:
        a = e[s] | e[t]
        for j in a:
            e[j] = a

q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    if e[s] != None and t in e[s]:
        print('yes')
    else:
        print('no')

Python: from collections import defaultdict
import sys
sys.setrecursionlimit(1 << 25)

G = defaultdict(lambda: [])
n, m = map(int, input().split())
color = {key: None for key in range(n)}

for _ in range(m):
    s, t = 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 no in G[node]:
        dfs(no, co)
    return


# すべてのnodeを開始点としてdfsで領域わけしてみる
c = 1
for node in G.keys():
    dfs(node, c)
    c += 1

Q = int(input())
for _ in range(Q):
    s, t = list(map(int, input().split()))
    if color[s] and color[t] and color[s] == color[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(2*n):
    for j in range(n):
        for k in range(j+1, n):
            if G[j][k] == 1:
                for l in range(k+1, n):
                    if G[k][l] == 1:
                        G[j][l] = 1
                        G[l][j] = 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: from collections import deque


user_num,rer_num=map(int,input().split())
graph=[[] for loop in range(user_num)]

for loop in range(rer_num):
    user1,user2=map(int,input().split())
    graph[user1].append(user2)
    graph[user2].append(user1)

#group[user1]はuser1が属するグループ.同じグループに属するuser同士は互いに到達可能
group=[-1]*user_num

que=deque([])
que.append(0)
tmp_group=0
for user in range(user_num):
    if(group[user]==-1):
        que.append(user)
        group[user]=tmp_group
        while(1):
            user1=que.popleft()
            for user2 in graph[user1]:
                if(group[user2]!=-1):
                    que.append(user2)
                    group[user2]=tmp_group
            if(len(que)==0):
                tmp_group+=1
                break


for loop in range(int(input())):
    user1,user2=map(int(input()))
    if(group[user1]==group[user2]):
        print("yes")
    else:
        print("no")

Python: #coding:utf-8

class UnionFind:
    def __init__(self,n):
        self.parent=[i for i in range(n+1)]
        self.rank=[0]*(n+1)
    def find(self,x):
        if self.parent[x] == x: # if x is x`s root
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]
    def is_same_tree(self,x,y):
        return self.find(x) == self.find(y) 
    def union(self,x,y):
        x=self.find(x)
        y=self.find(y)
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1
        
def main():
    n=list(map(int,input().split()))
    union=UnionFind(n[0])
    friends=[]
    question=[]
    for i in range(n[1]):
        a=tuple(map(int,input().split()))
        friends.append(a)
    for i in friends:
        union.union(i[0],i[1])
    m=int(input())
    for i in range(m):
        b=tuple(map(int,input().split()))
        question.append(b)
    for i in question:
        if union.find(i[0]) == union.find(i[1]):
            print("yes")
        else:
            print("no")
if __name__ == "__main__":
    main()

Python: from collections import deque

def bfs(graph, start):
    visited = {start: None}
    unvisited = deque()
    unvisited.append(start)
    while unvisited:
        now = unvisited.popleft()
        for next in graph[now]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    for key in visited.keys():
        graph[key].update(visited.keys())

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

for i in range(m):
    a, b = map(int, input().split())
    g[a].add(b)
    g[b].add(a)

q = int(input())

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

Python: from collections import deque


def bfs(adj_list, n):
    color = [None] * n
    color_count = 0
    searching = deque()
    for i in range(n):
        if color[i] is None:
            color_count += 1
            color[i] = color_count
            searching.append(i)
            while len(searching) > 0:
                start = searching.popleft()
                for end in adj_list[start]:
                    if color[end] is None:
                        color[end] = color_count
                        searching.append(end)
    return color


def dfs(adj_list, n):
    color = [None] * n
    color_count = 0
    searching = deque()
    for i in range(n):
        if color[i] is None:
            color_count += 1
            color[i] = color_count
            searching.append(i)
            while len(searching) > 0:
                start = searching.pop()
                for end in adj_list[start]:
                    if color[end] is None:
                        color[end] = color_count
                        searching.append(end)
    return color


def main():
    nm = input().split()
    n, m = list(map(int, nm))
    adj_list = [[] for _ in range(n)]
    for _ in range(m):
        st = input().split()
        s, t = list(map(int, st))
        adj_list[s].append(t)
        adj_list[t].append(s)
    color = dfs(adj_list, n)
    q = int(input())
    for _ in range(q):
        st = input().split()
        s, t = list(map(int, st))
        if color[s] == color[t]:
            print("yes")
        else:
            print("no")


if __name__ == "__main__":
    main()