Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     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() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		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()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

Python: class ConnectedComponents():
    def __init__(self, n):
        self.size = [1] * n
        self.id = [i for i in range(n + 1)]

    def root(self, i):
        while i != self.id[i]:
            self.id[i] = self.id[self.id[i]]
            i = self.id[i]

        return i

    def relations(self, s, t):
        s_root = self.root(s)
        t_root = self.root(t)

        if s_root == t_root: 
            return

        if self.size[s_root] < self.size[t_root]:
            self.id[s_root] = t_root
            self.size[t_root] += self.size[s_root]
        else:
            self.id[t_root] = s_root
            self.size[s_root] += self.size[t_root]

    def find(self, s, t):
        return self.root(s) == self.root(t)

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

CC = ConnectedComponents(n)

for _ in range(m):

    s, t = map(int, input().split())

    CC.relations(s, t)

# QUESTION
q = int(input())

for _ in range(q):

    s, t = map(int, input().split())

    if CC.find(s, t):
        print("yes")
    else:
        print("no")

Python: from collections import defaultdict


n, m = map(int, input().split())
rel = defaultdict(set)

for _ in range(m):
    p, r = map(int, input().split())
    rel[p].add(r)
    rel[r].add(p)


color = defaultdict(int)

def search(f, c):
    stack = [f]
    color[f] = c
    while stack:
        u = stack.pop()
        for v in rel[u]:
            if not color[v]:
                color[v] = c
                stack.append(v)


# assign color
cl = 0
for i in range(n):
    if not color[i]:
        cl += 1
        search(i, cl)

qn = int(input())

for _ in range(qn):
    f, l = map(int, input().split())
    print(['no', 'yes'][color[f] == color[l]])

Python: import sys
sys.setrecursionlimit(200000)

def dfs(v):
    global node_chk
    global B
    global connect
    global value

    node_chk[v] = 1
    connect[v] = value
    for x in B[v]:
        if(node_chk[x] == 0):
                dfs(x)



n,m = map(int,input().split())
B = [[]for i in range(n)] # 隣接リスト
node_chk = [0 for i in range(n)]
connect = [0 for i in range(n)]
value = 0

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

for i in range(n):
    if(node_chk[i] == 1):
        continue
    dfs(i)
    value += 1


q = int(input())
for i in range(q):
    start,target = map(int,input().split())
    if(connect[start] == connect[target]):
        print("yes")
    else:
        print("no")

Python: n = list(map(int,input().split()))
cn = [i for i in range(n[0])]
cnt = [[0]]*n[0]
for i in range(1,n[0]):
    cnt[i] = [i]
x = [1]*n[0]

for i in range(n[1]):
    a = list(map(int,input().split()))
    if cn[a[0]] != cn[a[1]]:
        if x[a[0]] > x[a[1]]: 
            tmp = cn[a[1]]
            tmp2 = cn[a[0]]
        else: 
            tmp = cn[a[0]]
            tmp2 = cn[a[1]]
        for j in cnt[tmp]:
            cn[j] = cn[tmp2]
        x[tmp2] += len(cnt[tmp])
        cnt[tmp2].extend(cnt[tmp])
        cnt[tmp] = [-1]

q = int(input())
for i in range(q):
    a = list(map(int,input().split()))
    if cn[a[0]] == cn[a[1]]: print("yes")
    else: print("no")

Python: # -*- coding: utf-8 -*-

def assignColor(G):
    n = len(G)
    C = [-1 for i in range(n)]
    for i in range(n):
        if C[i] == -1:
            C = BFS(G, i, C)
    return C

#@profile
def BFS(G, start, C):
    n = len(G)
    Q = [start]
    C[start] = start

    while len(Q) != 0:
        u = Q.pop(0)
        v = [i for i in G[u] if C[i] == -1] # v := ??£??\???????????????????????¢?´¢??????????????????
        
        for i in v:
            Q.append(i)
            C[i] = C[start]
    
    return C

def main():
    
    n, m = list(map(int, input().split()))
    G = [[] for i in range(n)]

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

    C = assignColor(G)
        
    q = int(input())
    
    for i in range(q):
        s, t = list(map(int, input().split()))
        if C[s] == C[t]:
            print("yes")
        else:
            print("no")
            
if __name__ == "__main__":
    main()

Python: import sys
sys.setrecursionlimit(1000000)

n,m=map(int,input().split())
graph=[[] for _ in range(n)]

for _ in range(m):
    a,b=map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)

def dfs(v):
    if reached[v]:
        reached[v]=0
        ll.add(v)
        for i in range(len(graph[v])):
            dfs(graph[v][i])

reached=[1]*n
ans=[[] for _ in range(n)]

for j in range(n):
    ll=set()
    dfs(j)
    if ll:
        for s in ll:
            ans[s]=ll

q=int(input())
ab=[]
for i in range(q):
    a,b=map(int,input().split())
    ab.append([a,b])

for i in range(q):
    a,b=ab[i][0],ab[i][1]
    if b in ans[a]:
        print("yes")
    else:
        print("no")

Python: def s():
 n,m=map(int,input().split())
 P=[[]for _ in[0]*n]
 C=[0]*n
 k=0
 for _ in[0]*m:
  s,t=map(int,input().split());P[s]+=[t];P[t]+=[s]
 for i in range(n):
  k+=1
  if C[i]<1:
   s=[i];C[i]=k
   while s:
    u=s.pop()
    for v in P[u]:
     if C[v]<1:C[v]=k;s+=[v]
 for _ in[0]*int(input()):
  a,b=map(int,input().split())
  print(['no','yes'][C[a]==C[b]])
if'__main__'==__name__:s()

Python: from collections import deque
from enum import Enum, auto
import sys
sys.setrecursionlimit(100000)

class Color(Enum):
    WHITE = auto()
    GRAY = auto()
    BLACK = auto()

n, m = list(map(int, input().split(' ')))
adj_list = [[] for i in range(n)]
for i in range(m):
    u, v = list(map(int, input().split(' ')))
    adj_list[u].append(v)
    adj_list[v].append(u)

# 幅優先
def adj_bfs(s, e):
    queue = deque([])
    colors = [Color.WHITE for i in range(n)]

    queue.append(s)
    colors[s] = Color.GRAY

    while len(queue) > 0:
        u = queue.popleft()
        for v in adj_list[u]:
            if colors[v] == Color.WHITE:
                colors[v] = Color.GRAY
                queue.append(v)
                if v == e: return True
        colors[u] = Color.BLACK
    return False

# 深さ優先
colors = [Color.WHITE for i in range(n)]

def adj_dfs(u, e):
    colors[u] = Color.GRAY

    for v in adj_list[u]:
        if colors[v] == Color.WHITE:
            if v == e: return True
            if adj_dfs(v, e):
                return True

    colors[u] = Color.BLACK
    return False

q = int(input())
for i in range(q):
    s, e = list(map(int, input().split(' ')))
    colors = [Color.WHITE for i in range(n)]
    if adj_dfs(s, e):
        print('yes')
    else:
        print('no')