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: import sys
from collections import defaultdict

def main():
    lines = sys.stdin.readlines()
    n, m = map(int, lines[0].split())
    relation_lines = lines[1:1+m]

    relations = defaultdict(list)

    for idx, relation_line in enumerate(relation_lines):
        s, t = relation_line.split()
        relations[s].append(t)
        relations[t].append(s)

    group = {}
    for key in relations.keys():
        group[key] = -1
    group_num = 0

    # assign a group number for ids
    for key, val in relations.items():
        if group[key] != -1:
            continue

        group[key] = group_num
        group_num += 1

        group_no = group[key]
        friends = [key]

        while len(friends) != 0:
            n_fs = relations[friends.pop()]
            for n_f in n_fs:
                if group[n_f] == -1:
                    group[n_f] = group_no
                    friends.append(n_f)

    q = int(lines[1+m])
    question_lines = lines[1+m+1:]
    answers = [None] * q
    for i, question in enumerate(question_lines):
        s, t = question.split()

        if s not in group or t not in group:
            answers[i] = 'no'
            continue
        elif group[s] == group[t]:
            answers[i] = 'yes'
        else:
            answers[i] = 'no'

    # answer
    [print(ans) for ans in answers]
    return

main()

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):
    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: 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[ch]
            if (v == False):
                color[u] = c
                S.append(u)
 
def assignColor():
    global c, color, G, n
    c = 1
    for i in range(n):
        if (color[i] == False):
            dfs(i, c)
            c += 1
 
n, m = list(map(int, input().split()))
 
G = [False 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: n, m = map(int, input().split())
rootList = [-1] * n

def getRoot(x):
    if rootList[x] < 0:
        return x
    rootList[x] = getRoot(rootList[x])
    return rootList[x]

def setSameRoot(x, y):
    x = getRoot(x)
    y = getRoot(y)
    if x != y:
        G[x], G[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")

Python: n, m = map(int, input().split())
graph = [[] for _ in range(n)]

global visited 
visited = [0 for _ in range(n)]

def input_graph(m):
  for _ in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

def dfs(node_id):
  
  for ad_node in graph[node_id]:
    if visited[ad_node] == 1:
      continue
    visited[ad_node] = 1
    dfs(ad_node)

if __name__ == "__main__":
  input_graph(m)
  q_num = int(input())
  for _ in range(q_num):
    visited = [0 for _ in range(n)]
    s, t = map(int, input().split())
    dfs(s)
    if visited[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]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

n, m = map(int, input().split())
g = [None]*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: #!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def assign_color():
    _color = 1
    for m in range(vertices_num):
        if vertices_status_list[m] == -1:
            graph_dfs(m, _color)
            _color += 1
    return None


def graph_dfs(vertex, color):
    vertices_stack = list()
    vertices_stack.append(vertex)
    vertices_status_list[vertex] = color
    while vertices_stack:
        current_vertex = vertices_stack[-1]
        vertices_stack.pop()
        for v in adj_list[current_vertex]:
            if vertices_status_list[v] == -1:
                vertices_status_list[v] = color
                vertices_stack.append(v)
    return None


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices_num, relation_num = map(int, _input[0].split())

    relation_info = _input[1:relation_num + 1]

    question_num = int(_input[relation_num + 1])
    question_list = _input[relation_num + 2:]

    adj_list = tuple([[] for _ in range(vertices_num)])
    vertices_status_list = [-1] * vertices_num

    for relation in relation_info:
        key, value = map(int, relation.split())
        adj_list[key].append(value)
        adj_list[value].append(key)

    assign_color()

    # print(adj_list)
    for question in question_list:
        start, target = map(int, question.split())
        if vertices_status_list[start] == vertices_status_list[target]:
            print("yes")
        else:
            print("no")

Python: def DFS(s):
    flag[s] = 1
    group[j] += s
    for i in range(n):
        if G[s] == i and flag[i] == 0:
                DFS(i)
 

n, m = map(int, input().split())
G = [[] for j in range(n)]
for i in range(m):
    s, t = map(int, input().split())
    G[s] += t
    G[t] += s

flag = [0 for i in range(n)]
group = []
j = 0
for i in range(n):
    if flag[i] == 0:
        group.append([])
        DFS(i)
        j += 1

q = int(input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, input().split())
    for j in range(len(group)):
        if s in group[j] and t in gruop[j]:
            ans[i] = "yes"
for i in range(q):
    print(ans[i])