Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

Python: # -*- coding: utf-8 -*-

def assignColor(G):
    n = len(G)
    C = [-1 for i in range(n)]
    for i in range(n):
        if C[i] == -1:
            C = BFS(G, i, C)
    return C
        
def BFS(G, start, C):
    n = len(G)
    flag = [0] * n
    Q = []

    Q.append(start)
    C[start] = start
    flag[start] = 1

    while len(Q) != 0:
        u = Q.pop(0)
        v = [i for i in G[u] if flag[i] == 0] # v := ??£??\???????????????????????¢?´¢??????????????????
        
        for i in v:
            Q.append(i)
            C[i] = C[start]
            flag[i] = 1
    
    return C

def main():
    
    n, m = list(map(int, input().split()))
    G = [[] 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)

    C = assignColor(G)
        
    q = int(input())
    
    for i in range(q):
        s, t = list(map(int, input().split()))
        if C[s] == C[t]:
            print("yes")
        else:
            print("no")
            
if __name__ == "__main__":
    main()

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(x) == f(y) else "no")
MAIN()

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