Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

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

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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