Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t 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() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		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()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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

def are_connected(s, t):
    q = [s]
    checked = [False] * n
    checked[s] = True

    while q:
        user = q.pop(0)
        for f in friend[user]:
            if f == t:
                return True
            elif not checked[f]:
                q.append(f)
                checked[f] = True
    return False

q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    if are_connected(s, t):
        print('yes')
    else:
        print('no')

Python: def read_friends():
    n, m = [int(x) for x in input().split()]
    adj_list = [set() for _ in range(n)]

    for _ in range(m):
        s, t = [int(x) for x in input().split()]
        adj_list[s].add(t)
        adj_list[t].add(s)

    return n, adj_list


def divide_group(n, adj_list):
    group = [None] * n

    def visit(k, g):
        S = [k]

        while S:
            k = S.pop()
            group[k] = g
            for t in adj_list[k]:
                if group[t] is None:
                    S.append(t)

    for k in range(n):
        if group[k] is None:
            visit(k, k)

    return group


def answer_queries(group):
    q = int(input())

    for _ in range(q):
        s, t = [int(x) for x in input().split()]
        print("yes" if group[s] == group[t] else "no")


def main():
    n, adj_list = read_friends()
    group = divide_group(n, adj_list)
    answer_queries(group)


if __name__ == '__main__':
    main()

Python: def dfs(u):
    global ct
    if color[u]!=-1:
        return
    color[u]=ct
    for v in fr[u]:
        if v!=-1 and color[v]==-1:
            dfs(v)
    color[u]=ct
    
n,m=map(int,raw_input().split())
fr=[[] for i in xrange(n)]
for i in xrange(m):
    s,t=map(int,raw_input().split())
    fr[s].append(t)
    fr[t].append(s)

q=input()
ct=0
color=[-1]*n
for i in xrange(n):
    dfs(i)
    ct+=1
for i in xrange(q):
    s,t=map(int,raw_input().split())
    if color[s]==color[t]:
        print('yes')
    else:
        print('no')

Python: n,m = map(int, input().split())
SNS = {i:[] for i in range(n)}
for i in range(m):
 s,t = map(int, input().split())
 SNS[s].append(t)
 SNS[t].append(s)

color = [None] * n
S = []
def dfs(r, c):
 S.append(r)
 color[r] = c
 while len(S) != 0:
  u = S.pop()
  for i in SNS[u]:
   if color[i] == None:
    color[i] = c
    S.append(i)
id = 0
for i in range(n):
 if color[i] == None:
  id += 1
  dfs(i, id)

q = int(input())
for i in range(q):
 s,t = map(int, input().split())
 if 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(n):
    for j in range(n):
        for k in range(j+1, n):
            if G[j][k] == 1:
                for l in range(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: def connect(o,s,t):
    global ct
    if color[s]!="white" or ct==1:
        return
    if fr[o][t]==t:
        ct+=1
    color[s]="grey"
    for i in fr[s]:
        if i==-1:
            continue
        elif i==t:
             ct+=1
             break
        else:
            fr[o][i]=i
            fr[i][o]=s
            connect(o,i,t)
            
n,m=map(int,raw_input().split())
fr=[[-1]*n for i in xrange(n)]
for i in xrange(m):
    s,t=map(int,raw_input().split())
    fr[s][t]=t
    fr[t][s]=s
q=input()
for i in xrange(q):
    ct=0
    color=["white"]*n
    s,t=map(int,raw_input().split())
    connect(s,s,t)
    if ct==0:
        print('no')
    else:
        print('yes')
        fr[s][t]=t
        fr[t][s]=s

Python: import sys
sys.setrecursionlimit(10000)
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: n, m = map(int, input().split())

a = [[] for i in range(n)]

for i in range(m):
    com = list(map(int, input().split()))
    a[com[0]].append(com[1])
    a[com[1]].append(com[0])

q = int(input())

from collections import deque
for j in range(q):
    queue = deque()
    memo = [0 for i in range(n)]
    data = list(map(int, input().split()))
    memo[data[0]] = 1
    queue.append(data[0])

    try:
        while len(queue):
            b = queue.popleft()
            for k in a[b]:
                if k == data[1]:
                    print("yes")
                    raise Exception
                if memo[k] == 0:
                    memo[k] = 1
                    queue.append(k)
        print("no")
    except Exception:
        pass