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: import sys
def s():
 e=sys.stdin.readline
 n,m=map(int,e().split())
 P=[[]for _ in[0]*n]
 C=[0]*n
 k=0
 for _ in[0]*m:
  s,t=map(int,e().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(e()):
  a,b=map(int,e().split())
  print(['no','yes'][C[a]==C[b]])
s()

Python: n_people, n_relationship = list(map(int, input().split()))
adj_list = [[] for _ in range(n_people)]
for i in range(n_relationship):
  p, target = list(map(int, input().split()))
  adj_list[p].append(target)
  adj_list[target].append(p)


group = [-1] * n_people
cnt = 0
for i in range(n_people):
  if group[i] == -1:
    group[i] = cnt
    stack = [i]
    while stack:
      at = stack.pop()
      for to in adj_list[at]:
        if group[to] == -1:
          group[to] = cnt
          stack.append(to)

    cnt += 1

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

Python: 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)                #sのお隣さんリストにtを
  edge[t].append(s)                #tのお隣さんリストにsを

group = [-1]*v                     #グループというリストを作る（こいつの値であとで友達か照合）
cnt = 0                            #グループ番号


#隣接リストに入っている同じエッジをスタックに入れてグループ化する
for i in range(v):
  if group[i] == -1:
    group[i] = cnt                 #カウントの値を入れる
    stack = [i]                    #エッジ番号を入れる
    while stack:
      v = stack.pop()              #エッジを代入　ー①
      for c in edge[v]:            #①のエッジのお隣さんリストを辿っていく
        if group[c] == -1:         #お隣さんのグループが初期化状態なら
          group[c] = cnt          #グループリストにカウントを入れる
          stack.append(c)          #スタックにお隣さんを入れる
    cnt += 1                       #お隣さんの訪問が終わったらグループ番号を変える

#クエリのコード
for _ in range(int(input())):
  s, t = map(int, input().split())
  print('yes' if group[s] == group[t] else '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):
    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: import sys
input = sys.stdin.readline

from collections import deque, defaultdict
from copy import deepcopy

MAX = 100000
NIL = -1

#グラフGはdictで管理する
G = defaultdict(lambda:deque())
color = [NIL]*MAX

#頂点r,探索ごとに異なる数cによって深さ優先探索をする関数
#つながっている部分は全て色が同じになる
def dfs(r, c):
    S = deque()
    S.append(r) 
    color[r] = c
    while S: #Sが存在する間以下の処理を実行
        u = S.popleft()
        for v in G[u]:#Gのvalueはqueueとなっている
            if color[v] == NIL:#色がない場合のみ以下の操作を行う
                color[v] = c
                S.append(v) #深さを深くして次の頂点を探す準備をする

def make_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)
    

make_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: def bfs(s, t):
  q = [s]
  visited = []
  while len(q) > 0:
    v = q.pop(0)
    if v == t: return True
    if v not in visited and len(G[v]) > 0:
      q.extend(sorted(G[v]))
      visited.append(v)
  return False

u, n = map(int, input().split())
G = [[] for _ in range(u)]
for _ in range(n):
  a, b = map(int, input().split())
  if b not in G[a]: G[a].append(b)
  if a not in G[b]: G[b].append(a)

q = int(input())
for _ in range(q):
  s, t = map(int, input().split())
  if bfs(s, t):
    print("yes")
  else:
    print("no")

Python: # coding: UTF-8
from collections import deque
n,m = map(int,input().split())
link = [[] for _ in range(n)]
part = [-1 for _ in range(n)]
for _ in range(m):
  u,v = map(int,input().split())
  link[u].append(v)
  link[v].append(u)

searched = set()
queue = deque()
pindex = 0
for u in range(n):
  if u not in searched:
    queue.append(u)
    pindex += 1
    searched.add(u)
    while queue:
      v = queue.popleft()
      part[v] = pindex
      for w in link[v]:
        if w not in searched:
          part[w] = pindex
          queue.append(w)
          searched.add(w)

q = int(input())
answers = []
for _ in range(q):
  s,t = map(int,input().split())
  if part[s] == part[t]:
    answers.append("Yes")
  else:
    answers.append("No")

for answer in answers:
  print(answer)

Python: n, m = map(int, input().split())
a = [[False]*n for i in range(n)]
rootList = [-1] * n

for i in range(m):
    s, t = map(int, input().split())
    if rootList[s] < 0:
        rootList[s] = s
    if rootList[t] < 0:
        rootList[t] = t
    if rootList[s] != rootList[t]:
        rootList[s] = min(rootList[s], rootList[t])
        rootList[t] = min(rootList[s], rootList[t])
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print("yes" if rootList[s] == rootList[t] != -1 else "no")