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: def tan(G,st,fi):
    global flag
    for i in range(len(G)):
        if G[st][i] == 1:
            if i == fi:
                flag = 1
            tan(G,i,fi)
    
n,m = list(map(int, input().split()))
G = [[0 for i in range(n)] for j in range(n)]

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

q = int(input())
ans = []
for i in range(q):
    Q = list(map(int, input().split()))
    st = Q[0]
    fi = Q[1]
    flag = 0
    tan(G,Q[0],Q[1])
    if flag==1:
        ans.append("yes")
    else:
        ans.append("no")
        
for i in range(q):
    print(ans[i])

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):
    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
    group.append([])

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: n, m = map(int, input().split())
rootList = [-1] * n
 
def getRoot(x):
    if rootList[x] < 0:
        rootList[x] = x
        return x
    return getRoot(rootList[x])
 
 
def setSameRoot(x, y):
    x = getRoot(x)
    y = getRoot(y)
    if x != y:
        rootList[x], rootList[y] = min(x, y), min(x, y)
 
for i in range(m):
    s, t = map(int, input().split())
    setSameRoot(s, t)
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print("yes" if getRoot(s) == getRoot(t) != -1 else "no")

Python: def main():
    n=list(map(int,input().split()))
    n = n[1]
    friends=[]
    question=[]
    for i in range(n):
        a=tuple(map(int,input().split()))
        friends.append(a)
    m=int(input())
    for i in range(m):
        b=tuple(map(int,input().split()))
        question.append(b)
    for i in question:
        if i in friends:
            print("yes")
        else:
            print("no")
if __name__ == "__main__":
    main()

Python: from collections import deque


def bfs(root):
    global time
    Q = deque()
    distance[root] = 0
    Q.append(root)
    i = root
    while len(Q) > 0:
        i = Q.popleft()
        for v in range(n):
            if v in adjDict[i] and color[v] == 'white':
                Q.append(v)
                color[v] = 'gray'
                distance[v] = distance[i] + 1
        color[i] = 'black'
        label[i] = time


firstline = list(map(int, input().split()))

n = firstline[0]
adjDict = {i: [] for i in range(n)}
color = ['white'] * n
distance = [-1] * n
label = [-1] * n

m = firstline[1]
for _ in range(m):
    adj = list(map(int, input().split()))
    adjDict[adj[0]].append(adj[1])
    adjDict[adj[1]].append(adj[0])

time = 0
for i in range(n):
    if color[i] == 'white':
        bfs(i)
        time += 1

q = int(input())
for _ in range(q):
    question = list(map(int, input().split()))
    if label[question[0]] == label[question[1]]:
        print('yes')
    else:
        print('no')

Python: from collections import defaultdict
import sys
sys.setrecursionlimit(1 << 25)  # 再起上限引き上げ

G = defaultdict(lambda: [])

#load data
N,M = map(int,input().split())
color = {key: None for key in range(N)}
#graphをdefaultdictで保持することにする
for _ in range(M):
    s,t = map(int,input().split())
    G[s].append(t)
    G[t].append(s)

#隣接リストを基にたどり着ける友達をグループ化する。
def dfs(node:int, co:int):
    #終了条件、既に訪問済みなら訪問しない
    if color[node] is not None:
        return
    #処理
    color[node] = co
    for n in G[node]:
        dfs(n,co)

    return

#全てのnodeを開始点としてdfsで色分けしてみる
c = 1
for node in G.keys():
    dfs(node,c)
    c += 1

#問題への解答
Q = int(input())
for _ in range(Q):
    s,t = map(int,input().split())
    if (color[s] is not None) and (color[t] is not None) and (color[s] == color[t]):
        print('yes')
    else:
        print('no')

Python: #Union Find

#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

#xとyが同じ集合に属するかの判定
def same(x,y):
    return find(x) == find(y)

#xが属する集合の個数
def size(x):
    return -par[find(x)]

#初期化
#根なら-size,子なら親の頂点
n,m = map(int,input().split())
par = [-1]*n
for i in range(m):
  x,y = map(int,input().split())
  unite(x,y)

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

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

def are_connected(s, t):
    q = [s]
    checked = [False] * n
    checked[s] = True

    while q:
        user = q.pop(0)
        for f in friend[user]:
            if f == t:
                print(f, user)
                return True
            elif not checked[f]:
                q.append(f)
                checked[f] = True
    return False

q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    if are_connected(s, t):
        print('yes')
    else:
        print('no')

Python: import sys
import os
import pprint

#fd = os.open('ALDS1_11_D-3.txt', os.O_RDONLY)
#os.dup2(fd, sys.stdin.fileno())

"""connected components"""

l = list(map(int, input().split()))
DEBUG = False

# node num
n = l[0]

# relation num
m = l[1]

# each node color
colors = [None] * n

rel_list = []
for _ in range(n):
    rel_list.append([])
# relation
for _ in range(m):
    l = list(map(int, input().split()))
    s = l[0]
    t = l[1]
    rel_list[s].append(t)
    rel_list[t].append(s)

if DEBUG:
    print(rel_list)

# coloring by DFS or BFS
for node_index in range(n):

    stack = []
    if colors[node_index] is None:
        stack.append(node_index)
        while stack:
            # process top of stack
            top = stack.pop()
            colors[top] = node_index

            relations = rel_list[top]
            for relation in relations:
                if colors[relation] is None:
                    stack.append(relation)

if DEBUG:
    print(colors)

# question num
q = int(input())

for _ in range(q):
    l = list(map(int, input().split()))
    s = l[0]
    t = l[1]
    if colors[s] == colors[t]:
        print('yes')
    else:
        print('no')