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: from collections import deque

n, m = map(int, input().split())
g = [set() for _ in range(n)]

while m:
    s, t = map(int, input().split())
    g[s].add(t)
    g[t].add(s)
    m -= 1


def find(s, t):
    global g
    queue = deque()
    queue.append((0, s))
    queue.append((1, t))
    visited = ([0] * n, [0] * n)
    visited[0][s] = 1
    visited[1][t] = 1

    while queue:
        st, i = queue.popleft()
        for f in g[i]:
            if visited[not st][f]:
                g[s].add(t)
                g[t].add(s)
                return True
            if not visited[st][f]:
                visited[st][f] = 1
                queue.append((st, f))
    return False


q = int(input())
while q:
    s, t = map(int, input().split())
    print('yes' if find(s, t) else 'no')
    q -= 1

Python: class UnionSet:
    def __init__(self, num_node: int) -> None:
        self.size = [1] * num_node
        self.id = [i for i in range(num_node + 1)]

    def get_root(self, i: int) -> int:
        while i != self.id[i]:
            self.id[i] = self.id[self.id[i]]
            i = self.id[i]
        return i

    def is_connected(self, node1: int, node2: int) -> bool:
        return self.get_root(node1) == self.get_root(node2)

    def unite(self, node1: int, node2: int) -> None:
        i, j = self.get_root(node1), self.get_root(node2)
        if i == j:
            return
        if self.size[i] < self.size[j]:
            self.id[i] = j
            self.size[j] += self.size[i]
        else:
            self.id[j] = i
            self.size[i] += self.size[j]


if __name__ == "__main__":
    num_users, num_relations = map(lambda x: int(x), input().split())
    union_set = UnionSet(num_users)

    for _ in range(num_relations):
        user1, user2 = map(lambda x: int(x), input().split())
        union_set.unite(user1, user2)

    num_questions = int(input())
    for _ in range(num_questions):
        user1, user2 = map(lambda x: int(x), input().split())
        if union_set.is_connected(user1, user2):
            print("yes")
        else:
            print("no")

Python: def search(s, t, a):
    global g
    a.add(s)
    if s == t:
        return True
    else:
        for ch in g[s]:
            if not (ch in a):
                result = search(ch, t, a)
                if result:
                    return result
        return False

n, m = list(map(int, input().split()))

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

q = int(input())
for i in range(q):
    s, t = list(map(int, input().split()))
    a = set()
    result = search(s, t, a)
    if result:
        print("yes")
    else:
        print("no")
    a.clear()

Python: 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)

isVisited = [False] * n

CC = [None] * n


def dfs(u, group):
    isVisited[u] = True
    CC[u] = group
    for v in adj[u]:
        if not isVisited[v]:
            dfs(v, group)

def makeCC(group):
    for i in range(n):
        if not isVisited[i]:
            dfs(i, group)
            group += 1

def isConnected(v1, v2):
    if CC[v1] == CC[v2]:
        print('yes')
    else:
        print('no')


makeCC(0)


q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    isConnected(s, t)

Python: 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

for i in range(n):
    for j in range(n):
        for k in range(n):
            if G[j][k] == 1:
                for l in range(n):
                    if G[k][l] == 1:
                        G[j][l] = 1
                        G[l][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 G[s][t] == 1:
        ans[i] = "yes"
for i in range(q):
    print(ans[i])

Python: n,m = map(int,input().split())
B = [[0 for i in range(n)] for j in range(n)]
chk = [0 for i in range(n)]
s = []
i_array = [0 for i in range(n)]
connect = []
cou = 0
node_in = [0 for i in range(n)]

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

for i in range(n):
    if(chk[i] == 1):
        continue
    chk[i] = 1
    s.append(i)
    connect.append([])
    connect[cou].append(i)
    node_in[i] = cou

    while(len(s) != 0):
        flag = 0
        v = s[len(s)-1]
        for j in range(i_array[v],n):
            if(B[v][j] == 1 and chk[j] == 0):
                chk[j] = 1
                s.append(j)
                connect[cou].append(j)
                node_in[j] = cou
                i_array[v] = j+1
                flag = 1
                break
        if(flag == 0):
            s.pop()

    cou += 1

q = int(input())
for i in range(q):
    start,target = map(int,input().split())
    p = node_in[start]
    flag = 0
    for j in range(len(connect[p])):
        if(connect[p][j] == target):
            print("yes")
            flag = 1
            break
    if(flag == 0):
        print("no")

Python: def tan(G,st,fi):
    global flag
    global flag_list
    for i in range(len(G)):
        if flag_list[i] == 1:
            continue
        if G[st][i] == 1:
            if i == fi:
                flag = 1
            flag_list[i] = 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
    G[S[1]][S[0]] = 1

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