Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

Python: import sys

sys.setrecursionlimit(10 ** 5 * 2)

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: # がんばったけどTLE
n, m = [int(i) for i in input().split()]
G = [[] for i in range(n)]
for mi in range(m):
    u, v = [int(i) for i in input().split()]
    G[u].append(v)
    G[v].append(u)
NIL = -1
Group = [NIL for i in range(n)]
num = 0
S = []
D = []


def DFS(id, u):
    S.append(u)
    Group[u] = id
    while S != []:
        for v in G[u]:
            if Group[v] == NIL:
                S.append(v)
                Group[v] = id
        u = S.pop()

'''
for num in range(n):
    if NIL not in Group:
        break
    else:
        u = Group.index(NIL)
        DFS(num, u)
'''
num = 0
q = int(input())
for qi in range(q):
    s, t = [int(i) for i in input().split()]
    if Group[s] == NIL:
        DFS(num, s)
        num += 1
    if Group[s] == Group[t]:
        print("yes")
    else:
        print("no")

Python: from collections import deque
from enum import Enum, auto

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')

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

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

Q = int(input())

for _ in range(Q):
    found = "no"
    start, target = tuple(map(int, input().split()))
    que = deque([start])
    searched = set([start])
    while len(que):
        q = que.pop()
        next_ = [i for i, a in enumerate(adj[q]) if (a==1) and not (i in searched)]
        searched.update(next_)
        que.extendleft(next_)
        if target in next_:
            found = "yes"
            break
    for a, b in combinations(searched, 2):
        adj[a][b] = 1
        adj[b][a] = 1
    print(found)

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()))
    print("yes" if cn[a[1]] == cn[a[0]] else "no")

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

adj = [[] for i in range(n)]

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

isVisited = [False] * n

CC = [None] * n

import sys
sys.setrecursionlimit(1000000000)

def dfs(u, group):
    isVisited[u] = True
    CC[u] = group
    for v in adj[u]:
        if not isVisited[v]:
            dfs(v, group)

def makeCC(group):
    for i in range(n):
        if not isVisited[i]:
            dfs(i, group)
            group += 1

def isConnected(v1, v2):
    if CC[v1] == CC[v2]:
        print('yes')
    else:
        print('no')


makeCC(0)


q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    isConnected(s, t)

Python: import sys
sys.setrecursionlimit(200000)


def dfs(v, cnt):
    group[v] = cnt
    for c in edge[v]:
        if group[c] == -1:
            dfs(c, cnt)

V, E = map(int, input().split())
edge = [[] for _ in range(V)]
for _ in range(E):
    s, t = map(int, input().split())
    edge[s].append(t)
    edge[t].append(s)
group = [-1] * V
c = 0
for i in range(V):
    if group[i] == -1:
        dfs(i, c)
        c += 1

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

Python: n,m = map(int,raw_input().strip().split(' '))
graph = [[] for i in range(n)]
for i in range(m):
    s,t = map(int,raw_input().strip().split(' '))
    graph[s].append(t)
    graph[t].append(s)
 
color = {}
c = 0
for i in range(n):
    if not i in color:
        que = [i]
        color[i] = c
        if len(que) > 0:
         for j in range(n):
            u = que[0]
            for v in graph[u]:
                if not v in color:
                    color[v] = c
                    que.append(v)
            del que[0]
        c += 1