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

Python: 
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i in range(self.n) if self.parents[i] < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return "\n".join("{}: {}".format(r, self.members(r)) for r in self.roots())


n,m = map(int,input().split())
tree = UnionFind(n)
for _ in range(m):
    a,b = map(int,input().split())
    tree.union(a,b)

q = int(input())
for i in range(q):
    a,b = map(int,input().split())
    if tree.same(a,b):
        print("yes")
    else:
        print("no")