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

UNVISITED=0
ENTERED=1
VISITED=2

def nextNL(u, nList, state):
    l=nList[u]
    for i in l:
        if(state[i]==UNVISITED):
            return i
    return -1

def dfs(s,t,nList,state, color,icol):

    S=[]

    S.append(s)
    state[s]=ENTERED
    color[s]=icol
    while(len(S)>0):
        current=S[-1]
        next=nextNL(current, nList, state)
        if next!=-1:
            S.append(next)
            state[next]=ENTERED
            color[next]=icol
        else:
            state[S.pop()]=VISITED

    return color[s]==color[t]

def main():

    n,m=list(map(int,input().split()))
    nList=[]
    for _ in range(n):
        nList.append([])

    for _ in range(m):
        s,t=list(map(int,input().split()))
        nList[s].append(t)
        nList[t].append(s)
    
    state=[UNVISITED]*n

    q=int(input())

    icol=-1
    color=[icol]*n
    for _ in range(q):
        s,t=list(map(int,input().split()))
        if(color[s] == -1) and (color[t] == -1):
            icol+=1
            res=dfs(s,t,nList,state,color,icol)
            if(res==True):
                print('yes')
            else:
                print('no')
        elif (color[s]==-1) or (color[t]==-1):
            print('no')
        elif (color[s] == color[t]):
            print('yes')
        else:
            print('no')
    
if __name__=='__main__':
    main()

Python: n, m = [int(i) for i in input().split()]
G = [[] for _ in range(n)]
for mi in range(m):
    u, v = [int(i) for i in input().split()]
    G[u].append(v)
    G[u] += G[v]
    G[v].append(u)
    G[v] += G[u]

q = int(input())
for qi in range(q):
    s, t = [int(i) for i in input().split()]
    if t in G[s] or s in G[t]:
        print("yes")
    else:
        print("no")

Python: from collections import deque

def bfs(graph, start, goal, connection):
    visited = {start: None}
    unvisited = deque(start)
    while unvisited:
        now = unvisited.popleft()
        if now == goal or (1000000*now + goal) in connection:
            connection[1000000*start + goal] = 1
            connection[1000000*goal + start] = 1
            return "yes"
        for next in graph[now]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

n, m = map(int, input().split())
g = [[] for i in range(n)]
c = {}

for i in range(m):
    inp = list(map(int, input().split()))
    g[inp[0]].append(inp[1])
    g[inp[1]].append(inp[0])
    c[1000000*inp[0] + inp[1]] = 1
    c[1000000*inp[1] + inp[0]] = 1

q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    print(bfs(g, s, t, c))

Python: from collections import deque

def bfs(graph, start, goal, connection):
    visited = {start: None}
    unvisited = deque()
    unvisited.append(start)
    while unvisited:
        now = unvisited.popleft()
        if now == goal or goal in connection[now]:
            connection[start].add(goal)
            connection[goal].add(start)
            return "yes"
        for next in graph[now]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

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

for i in range(m):
    inp = list(map(int, input().split()))
    g[inp[0]].add(inp[1])
    g[inp[1]].add(inp[0])
    c[inp[0]].add(inp[1])
    c[inp[1]].add(inp[0])

q = int(input())

for i in range(q):
    s, t = map(int, input().split())
    print(bfs(g, s, t, c))

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
    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: import sys

sys.setrecursionlimit(1000000)

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
    visited = [0] * n

    def dfs(i):
        visited[i] = 1
        if i == t:
            return True
        for f in g[i]:
            if not visited[f]:
                if dfs(f):
                    return True
        return False

    return dfs(s)


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

Python: # -*- coding: utf-8 -*-
import sys

def assignColor():
    for i in range(n):
        if C[i] == -1:
            DFS(i)

#@profile
def DFS(start):
    S = [start]
    C[start] = start
    while len(S) != 0:
        u = S.pop(0)
        for i in adj_list[u]:
            if C[i] == -1:
                C[i] = start
                S.append(i)

if __name__ == "__main__":
    _input = sys.stdin.readlines()
    n, m = list(map(int, _input[0].split()))
    adj_list = [[] for i in range(n)]
    C = [-1] * n
    
    for i in range(m):
        s, t = list(map(int, _input[i+1].split()))
        adj_list[s].append(t)
        adj_list[t].append(s)
    assignColor()
    q = int(_input[m+1])
    for i in range(q):
        s, t = list(map(int, _input[m+i+2].split()))
        if C[s] == C[t]:
            print("yes")
        else:
            print("no")