Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t 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() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		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()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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

friend = [ [] for _ in range(n) ]

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

flag = [0] * n
group = 0

# def dfs(u):
#    for v in friend[u]:
#        if flag[v] == 0:
#            flag[v] = group
#            dfs(v)

for i in range(n):
    if flag[i] == 0:
        group += 1
        flag[i] = group
        # dfs(i)
        
        stack = [i]
        
        while stack:
            u = stack.pop()
            for v in friend[u]:
                if flag[v] == 0:
                    flag[v] = group
                    stack.append(v)

q = int(input())

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

Python: import sys
from collections import deque
input = sys.stdin.readline

V, E = map(int, input().split())
G = {i: deque([]) for i in range(V)}
for _ in range(E):
    v1, v2 = map(int, input().split())
    G[v1].append(v2)
    G[v2].append(v1)


seen = [0]*(V)
stack = []
color = {v: -1 for v in range(V)}

def dfs(j, col):
    global time
    if seen[j]:
        return
    seen[j] = 1
    stack.append(j)
    color[j] = col
    while stack:
        s = stack[-1]
        if not G[s]:
            stack.pop()
            continue
        g_NO  = G[s].popleft()
        if seen[g_NO]:
            continue
        color[g_NO] = col
        seen[g_NO] = 1
        stack.append(g_NO)
    
c = 0
for start in range(V):
    if seen[start]:
        continue
    dfs(start, c)
    c += 1


q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    if color[s] != -1 and color[t] != -1 and color[s] == color[t]:
        print("yes")
    else:
        print("no")

Python: n,m = map(int,input().split())
Rel = [tuple(map(int,input().split())) for _ in range(m)]
q = int(input())
Que = [tuple(map(int,input().split())) for _ in range(q)]

class UF:
    def __init__(self):
        self.roots = [i for i in range(n)]
        self.rank = [1 for _ in range(n)]

    def same(self,a,b):
        if self.find(a) == self.find(b):
            return True
        else:
            return False

    def find(self,a):
        if self.roots[a] == a:
            return a
        else:
            self.roots[a] = self.find(self.roots[a])
            return self.roots[a]

    def union(self,a,b):
        a,b = self.find(a),self.find(b)
        if a != b:
            if self.rank[a] >= self.rank[b]:
                self.roots[b] = a
                if self.rank[a] == self.rank[b]:
                    self.rank[b] += 1
            else:
                self.roots[a] = b

uf = UF()

for i in range(m):
    uf.union(Rel[i][0],Rel[i][1])

for i in range(q):
    if uf.same(Que[i][0],Que[i][1]):
        print("yes")
    else:
        print("no")

Python: #!/usr/bin/python3
n, m = map(int, input().split())
ALL = [0] * n
Graph = []
for i in range(m):
    s, t = map(int, input().split())
    if ALL[s] == 0 and ALL[t] == 0:
        Graph.append(set([s, t]))
    elif ALL[s] == 0:
        for g in Graph:
            if t in g:
                g.add(s)
                break
    elif ALL[t] == 0:
        for g in Graph:
            if s in g:
                g.add(t)
                break
    else:
        sg = None
        tg = None
        for g in Graph:
            if s in g:
                sg = g
            if t in g:
                tg = g

        if sg is not tg:
            sg.update(tg)
            Graph.remove(tg)

    ALL[s] = 1
    ALL[t] = 1
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    for g in Graph:
        if s in g and t in g:
            print('yes')
            break
    else:
        print('no')

Python: #16D8104025G 佐藤智裕 mist75__ Python3
#alds11_d

global flag
flag = 0
"""
class Node:
    def __init__(self, num, deg, neib):
        self.num = num
        self.deg = deg
        self.neib = neib
"""
"""
class Link:
    def __init__(self, num, linker):
        self.num = num
        self.linker = linker

    def __str__(self):
        return str(self.num) + str(*self.linker)
"""

def search(U, i, j, checked):
    global flag
    flag = 0
    #print("i,j,flag:", i, j, flag)

    for l in U[i]:
        if flag == 1:
            break
        else:
            #print("l:",l)
            if l in checked:
                #print("has checked")
                pass
            elif l == j:
                #print("reached")
                flag = 1
                break
            else:
                checked.append(i)
                #print("checked:", checked)
                #print("next")
                search(U, l, j, checked)

    if flag == 0:
        #print("can't find")
        return "no"
    else: return "yes"


n, m = map(int, input().split()) #n:user number, m:connection number
User = []
for i in range(n):
    linker = []
    User.append(linker)

#print("User-linker set", User)
for i in range(m):
    lnk1, lnk2 = map(int, input().split())
    User[lnk1].append(lnk2)
    User[lnk2].append(lnk1)

#print("User-link input")
#print("User:",User)

#q = int(input("questions:"))
q = int(input())
Result = []
for i in range(q):
    checked = []
    i, j = map(int, input().split())
    Result.append(search(User, i, j, checked))
    #print(Result)

for i in Result:
    print(i)

Python: n = list(map(int,input().split()))
cn = [i for i in range(n[0])]
for i in range(n[1]):
    a = list(map(int,input().split()))
    if cn[a[0]] != cn[a[1]]:
        tmp = cn[a[1]]
        for j in range(n[0]):
            if cn[j] == tmp:
                cn[j] = cn[a[0]]

q = int(input())
for i in range(q):
    a = list(map(int,input().split()))
    if cn[a[0]] == cn[a[1]]: print("yes")
    else: print("no")