Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

Python: #coding:utf-8

def CC(root,dist):
    Flag=False
    if len(dist)!=0:
        for i in range(len(dist)):
            if dist[i][-1]==root[0]:
                d=dist[i]
                d=d+[root[1]]
                dist.append(d)
                Flag=True
            elif dist[i][-1]==root[1]:
                d=dist[i]
                d=d+[root[0]]
                dist.append(d)
                Flag=True
    if Flag==False:
        dist.append(root)

def Check(dist,qList):
    for i in range(len(dist)):
        if qList[0] in dist[i] and qList[1] in dist[i]:
            return "yes"
    return "no"
    
if __name__=="__main__":
    I=input()
    n=int(I.split(" ")[0])
    m=int(I.split(" ")[1])
    A=[]
    for i in range(m):
        A.append(list(map(int,input().split(" "))))
    dist=[]
    for i in range(m):
        CC(A[i],dist)
    q=int(input())
    for i in range(q):
        print(Check(dist,list(map(int,input().split(" ")))))

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


def solve():
    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")
    return vertices_status_list


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
    ans = solve()

Python: import sys

sys.setrecursionlimit(10 ** 7)

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def paint_col(adj, cols, i, col):
    cols[i] = col

    for child in adj[i]:
        if cols[child] == 0:
            paint_col(adj, cols, child, col)

def solve():
    n, m = map(int, input().split())
    adj = [[] for i in range(n)]

    for i in range(m):
        s, t = map(int, sys.stdin.readline().split())
        adj[s].append(t)
        adj[t].append(s)

    cols = [0] * n
    col = 1

    for i in range(n):
        if cols[i] == 0:
            paint_col(adj, cols, i, col)
            col += 1

    # debug(cols, locals())

    q = int(input())

    for i in range(q):
        s, t = map(int, sys.stdin.readline().split())

        if cols[s] == cols[t]:
            print('yes')
        else:
            print('no')

    pass

if __name__ == '__main__':
    solve()

Python: if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline

    from collections import deque, defaultdict
    from copy import deepcopy

    MAX = 100000
    NIL = -1

    # Gはdictで管理する、valueはdeque
    G = defaultdict(lambda: deque())
    color = [NIL]*MAX

    def dfs(r, c):
        S = deque()
        S.append(r)
        color[r] = c
        while S:
            u = S.popleft()
            for v in G[u]:  # Gのvalueはdequeなので、それをvに一つずつ入れていく
                if color[v] == NIL:
                    color[v] = c
                    S.append(v)

    def assign_color():
        id = 1
        for u in range(n):
            if color[u] == NIL:
                dfs(u, id)
                id += 1

    n, m = map(int, input().split())

    for _ in range(m):
        s, t = map(int, input().split())
        G[s].append(t)
        G[t].append(s)

    assign_color()

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

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
        else:
            f2(li[a], b)
            li[a] = b
    def solve(a, b):
        if f(a) == f(b):
            return "yes"
        return "no"
    for _ in range(m):
        x, y = map(int, readline().split())
        if f(x) > f(y):
            f2(x, li[y])
        else:
            f2(y, li[x])
    for _ in range(int(input())):
        x, y = map(int, readline().split())
        print(solve(x, y))
MAIN()

Python: from queue import deque

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

def dfs(r, c):
    S = deque([r])
    color[r] = c
    while (len(S)):
        u = S.popleft()
        for i in [j for j in range(0, m) if G[u][j] == 1]:
            if color[i] == -1:
                color[i] = c
                S.append(i) # ???????????£?????????????????????????????¢???

def assginColor():
    i_d = 1
    for u in range(n):
        if color[u] == -1:
            dfs(u, i_d)
            i_d += 1

assginColor()
q = int(input())

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