Go: package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

Python: class UnionFind:
    def __init__(self,n):
        self.n = n
        self.p = [i for i in range(n)]
        self.rank = [0]*n

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

    def root(self,x):
        if x!=self.p[x]:self.p[x] = self.root(self.p[x])
        return self.p[x]

    def unite(self,x,y):
        x = self.root(x)
        y = self.root(y)
        if self.rank[y]<self.rank[x]:
            self.p[y] = x
        else:
            self.p[x] = y
            if self.rank[y]==self.rank[x]:
                self.rank[y]+=1

def main():
    n,m = map(int,input().split())
    UF = UnionFind(n)
    for _ in range(m):
        a,b = map(int,input().split())
        UF.unite(a,b)
    q = int(input())
    for _ in range(q):
        a,b = map(int,input().split())
        if UF.same(a,b):print ('yes')
        else           :print ('no')


if __name__ == '__main__':
    main()

Python: from collections import deque


user_num,rer_num=map(int,input().split())
graph=[[] for loop in range(user_num)]

for loop in range(rer_num):
    user1,user2=map(int,input().split())
    graph[user1].append(user2)
    graph[user2].append(user1)

#group[user1]はuser1が属するグループ.同じグループに属するuser同士は互いに到達可能
group=[-1]*user_num

que=deque([])
que.append(0)
tmp_group=0
for user in range(user_num):
    if(group[user]==-1):
        que.append(user)
        group[user]=tmp_group
        while(1):
            user1=que.popleft()
            for user2 in graph[user1]:
                if(group[user2]==-1):
                    que.append(user2)
                    group[user2]=tmp_group
            if(len(que)==0):
                tmp_group+=1
                break

for loop in range(int(input())):
    user1,user2=map(int,input().split())
    if(group[user1]==group[user2]):
        print("yes")
    else:
        print("no")

Python: from collections import defaultdict
user_num, conn_num = (int(n) for n in input().split(" "))
conn = defaultdict(list)
for _ in range(conn_num):
    i, j = (int(n) for n in input().split(" "))
    conn[i].append(j)
    conn[j].append(i)
find = [-1 * n - 1 for n in range(user_num)]
query = int(input())
for q_num in range(query):
    one, theother = (int(n) for n in input().split(" "))
    if find[one] != find[theother] and find[one] < 0:
        queue = [one]
        find[one] = q_num
        while queue:
            new_queue = []
            for q in queue:
                for c in conn[q]:
                    if find[c] < 0:
                        new_queue.append(c)
                        find[c] = q_num
            queue = new_queue
    if find[one] == find[theother]:
        print("yes")
    else:
        print("no")

Python: from collections import deque


def CC():
    id=0
    color=["white" for i in range(n)]
    for j in range(n):
        if color[j]=="white":
            color[j]="gray"
            queue=deque([j])

            while len(queue)!=0:
                u=queue.popleft()
                for i in M[u]:
                    if color[i]=="white":
                        color[i]="gray"
                        queue.append(i)
                color[u]=str(id)
            id +=1

    return color


n,m=map(int,input().split())
M=[[] for i in range(n)]
for i in range(m):
    p,q=map(int,input().split())
    M[p].append(q)
    M[q].append(p)

color=CC()

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

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

G = defaultdict(lambda: [])

# load data
N, M = list(map(int, input().split()))
color = {key: None for key in range(N)}  # 色というかグループ #intで表す #Noneは未訪問を示す
# graphをdefault dictで保持することにする。
for _ in range(M):
    s, t = list(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)  # 訪問済みだったら色を塗り替えないというないようはdfs関数内の処理で吸収できる
    c += 1

# 問題に回答していく
Q = int(input())
for _ in range(Q):
    s, t = list(map(int, input().split()))
    if ((color[s] is not None) and (color[t] is not None)) and (color[s] == color[t]):
        # s,tも友達がいて、かつ同じグループだったら
        print('yes')
    else:
        print('no')

Python: import sys
input_lines = [[int(i) for i in line.split()] for line in sys.stdin.read().splitlines()]
[user_num, rel_num] = input_lines[0]
rel_list = input_lines[1:rel_num+1]
ques_num = input_lines[rel_num+1][0]
ques_list = input_lines[rel_num+2:]
color_list = [-1] * user_num
adja_list = [set() for i in range(user_num)]

def dfs(id, color):
    if color_list[id] == -1:
        color_list[id] = color
        for a_id in adja_list[id]:
            dfs(a_id, color)



for rel in rel_list:
    adja_list[rel[0]].add(rel[1])
    adja_list[rel[1]].add(rel[0])

color = 0
for id in range(user_num):
    print(color_list)
    dfs(id, color)
    color += 1

for ques in ques_list:
    if color_list[ques[0]] == color_list[ques[1]]:
        print('yes')
    else:
        print('no')