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: #!/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")

Python: n, m = map(int, input().split())
link = [[] for _ in range(n)]
for _ in range(m):
    u, v = map(int, input().split())
    link[u].append(v)
    link[v].append(u)
group = [-1] * n
gid = 0
for i in range(n):
    if group[i] != -1:
        continue
    stack = [i]
    group[i] = gid
    while stack:
        u = stack.pop()
        for v in link[u]:
            if group[v] == -1:
                group[v] = gid
                stack.append(v)
    gid += 1
q = int(input())
for _ in range(q):
    u, v = map(int, input().split())
    print('yes' if group[u] == group[v] else 'no')

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

# dfsで連結成分を求める
r = [0]*n
for i in range(n):
    if r[i] == 0:
        l = [i]
        while len(l) > 0:
            u = l.pop(-1)
            r[u] = i+1
            for v in g[u]:
                if r[v] == 0:
                    l.append(v)

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