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: def DFS(s):
    group[s] = j
    for i in range(n):
        if i in G[s] and group[i] == 0:
            DFS(i)

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

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

group = [0 for i in range(n)]
j = 1
for i in range(n):
    if group[i] == 0:
        DFS(i)
        j += 1

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

Python: from collections import deque
 
n, m = map(int, input().split())

adj = [[] for _ in range(n)] # adjacent list

for _ in range(m):
    u, v = map(int, input().split())
    adj[u].append(v)
    adj[v].append(u)

c = ['w']*n # color of each vertex
con = [0]*n
cnt = 0


def BFS_visit(s):
    global c
    global cnt
    global con
    
    c[s] = 'g'
    con[s] = cnt
    
    Q = deque()
    Q.append(s)
    while Q:
        u = Q.popleft()
        for v in adj[u]:
            if c[v] == 'w':
                c[v] = 'g'
                con[v] = cnt
                Q.append(v)
        c[u] = 'b'
        
for u in range(n):
    if c[u] == 'w':
        cnt += 1
        BFS_visit(u)
        
        
q = int(input())
for _ in range(q):
    u, v = map(int, input().split())
    ans = "yes" if (con[u] == con[v]) else "no"
    print(ans)

Python: def main():
    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)

    q = int(input())
    component_ids = [-1 for i in range(n)]
    id = 0
    for i in range(n):
        if component_ids[i] != -1:
            continue
        bfs(i, id, adj, component_ids)
        id += 1

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


def bfs(node, id, adj, component_ids):
    que = [node]
    while que:
        u = que.pop(0)
        component_ids[u] = id
        for v in adj[u]:
            if component_ids[v] == -1:
                que.append(v)

main()

Python: from collections import deque
n,m = [int(i) for i in input().split()]
F = [{} for i in range(n)]
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
DP = [0 for i in range(n)]
c = 1
d = deque()
for i in range(n):
    if not DP[i]:
        d.append(i)
    while(len(d)>0):
        tmp = d.pop()
        DP[tmp] = c
        for i in F[tmp]:
            if not DP[i]:
                d.append(i)
    c+=1

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

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

group = []
c = 0
for i in range(n):
    group.append([])
    if not i in group:
        que = [i]
        group[i] = c
        while len(que) > 0:
            u = que[0]
            for j in G[u]:
                if not j in group:
                    group[j] = que.append(j)
            del que[0]
        c += 1

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

Python: 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: n, m = map(int, input().split())
G = [[] for j in range(n)]
for i in range(m):
    s, t = map(int, input().split())
    G[s].append(t)
    G[t].append(s)

group = [[] for i in range(n)]
c = 0
for i in range(n):
    if not i in group:
        que = [i]
        group[i] = c
        while len(que) > 0:
            u = que[0]
            for j in G[u]:
                if not j in group:
                    group[j] = que.append(j)
            del que[0]
        c += 1

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

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


class LinkedGraph:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def check(self, start, goal):
        states = [1] * self.n
        queue = deque([start])

        while queue:
            u = queue.popleft()
            states[u] = 0

            for v in self.ll[u]:
                if v == goal:
                    print('yes')
                    return
                if states[v]:
                    queue.append(v)
        else:
            print('no')
            return


if __name__ == '__main__':
    n, m = map(int, input().split())
    g = LinkedGraph(n)

    for _ in range(m):
        u, v = map(int, input().split())
        g.link(u, v)

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        g.check(u, v)

Python: def DFS(s):
    flag[s] = 1
    group[s] = j
    for i in range(n):
        if G[s][i] == 1 and flag[i] == 0:
            DFS(i)

n, m = map(int, raw_input().split())
G = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    s, t = map(int, raw_input().split())
    G[s][t] = 1
    G[t][s] = 1

group = [[] for i in range(n)]
flag = [0 for i in range(n)]
j = 0
for i in range(n):
    if flag[i] == 0:
        DFS(i)
        j += 1

q = int(raw_input())
ans = ["no" for i in range(q)]
for i in range(q):
    s, t = map(int, raw_input().split())
    if group[s] == group[t]:
        ans[i] = "yes"
for i in range(q):
    print(ans[i])

print(group)