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: import sys
class Union():
    def __init__(self, n):
        self.pre = [i for i in range(n)]
        self.rank = [1 for _ in range(n)]

    def union(self, a, b):
        aroot = self.find(a)
        broot = self.find(b)
        if aroot == broot:
            return
        if self.rank[aroot] < self.rank[broot]:
            aroot, broot = broot, aroot
        self.pre[broot] = self.pre[aroot]
        if self.rank[aroot] == self.rank[broot]:
            self.rank[aroot] += 1

    def find(self, n):
        while self.pre[n] != n:
            n = self.pre[n]
        return n

    def issame(self, a, b):
        if self.find(a) == self.find(b):
            return True
        return False

if __name__ == '__main__':
    m, n = map(int, sys.stdin.readline().split())
    union = Union(m)

    for _ in range(n):
        a, b = map(int, sys.stdin.readline().split())
        union.union(a, b)

    o = int(input())
    for _ in range(o):
        p, q = map(int, sys.stdin.readline().split())
        if union.issame(p, q):
            print('yes')
        else:
            print('no')

Python: def DFS(s):
    flag[s] = 1
    F[group].append(s)
    for i in range(n):
        if G[s][i] == 1:
            if flag[i] == 0:
                DFS(i)
 

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

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

q = int(input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, input().split())
    for friends in F:
        if s in friends and t in friends:
            ans[i] = "yes"

for i in range(q):
    print(ans[i])

Python: # -*- coding: utf-8 -*-
from collections import deque

N, M = tuple(map(int, input().split()))
adj = [[0]*(N) for _ in range(N)]

for _ in range(M):
    inp = tuple(map(int, input().split()))
    adj[inp[0]][inp[1]] = 1
    adj[inp[1]][inp[0]] = 1

unsearched = set(range(N))
colors = {n:-1 for n in range(N)}
color = -1
while len(unsearched):
    color += 1
    start = min(unsearched)
    unsearched.remove(start)
    searched = set([start])
    
    que = deque([start])
    while len(que):
        q = que.pop()
        next_ = [i for i in unsearched if adj[q][i]==1 and not (i in searched)]
        searched.update(next_)
        unsearched = unsearched.difference(searched)
        que.extendleft(next_)
    for n in searched:
        colors[n] = color
    
Q = int(input())

for i in range(Q):
    question = tuple(map(int, input().split()))
    if colors[question[0]] == colors[question[1]]:
        print("yes")        
    else:
        print("no")

Python: entry = map(int, raw_input())
n = entry[0]
m = entry[1]
A = [] * n
for i in range(m):
    entry = map(int, raw_input())
    A[entry[0]].append(entry[1])
    A[entry[1]].append(entry[0])

q = int(raw_input())
for i in range(q):
    entry = map(int, raw_input())
    if entry[1] in A[entry[0]]:
        print("yes")
    else:
        print("no")

Python: # -*- coding: utf_8 -*-

level = False
def debug(v):
    if level:
        print(v)

NOT_FOUND = 0
ENQUEUED = 1
FOUND = 2

def bfs(graph, start, target):
    debug(graph)
    if start == target:
        return True
    
    queue = []
    status = [NOT_FOUND] * len(graph)
    queue.append(start)

    while len(queue) != 0:
        debug("----------------------------")
        node = queue.pop(0)
        status[node] = FOUND
        edges = graph[node]
        debug(queue)
        debug(status)
        debug(edges)
        for e in edges:
            if e == target:
                return True
            if status[e] == NOT_FOUND:
                status[e] = ENQUEUED
                queue.append(e)

    return False

if __name__ == "__main__":
    n, m = [int(x) for x in input().split()]

    g = [[] for i in range(n)]
    for i in range(m):
        u, e = [int(x) for x in input().split()]
        g[u].append(e)
        g[e].append(u)

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

Python: #!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

n,m=map(int,input().split())
G=[[]*n for i in range(n)]
color=[-1]*n
for i in range(m):
    v1,v2=map(int,input().split())
    G[v1].append(v2)
    G[v2].append(v1)


def dfs(v,c):
    color[v]=c
    for vi in G[v]:
        if color[vi]==-1:
            dfs(vi,i)


for i in range(n):
    if color[i]==-1:
        dfs(i,i)

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

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def assign_color():
    _id = 1
    for m in range(vertices_num):
        if vertices_status_list[m] == -1:
            graph_dfs(m, _id)
            _id += 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 = [[] 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")