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: def connect(o,s,t):
    global ct
    if color[s]!="white" or ct!=0:
        return
    if fr[o][t]==t:
        ct+=1
    color[s]="grey"
    for i in fr[s]:
        if i==-1:
            continue
        elif i==t or ct!=0:
             ct+=1
             break
        else:
            fr[o][i]=i
            fr[i][o]=o
            connect(o,i,t)
            
n,m=map(int,raw_input().split())
fr=[[-1]*n for i in xrange(n)]
for i in xrange(m):
    s,t=map(int,raw_input().split())
    fr[s][t]=t
    fr[t][s]=s
q=input()
for i in xrange(q):
    ct=0
    color=["white"]*n
    s,t=map(int,raw_input().split())
    connect(s,s,t)
    if ct==0:
        print('no')
    else:
        print('yes')
        fr[s][t]=t
        fr[t][s]=s

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

a = [[] for i in range(n)]

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

q = int(input())

from collections import deque
queue = deque()


for j in range(q):
    memo = [0 for i in range(n)]
    data = list(map(int,input().split()))
    memo[data[0]] = 1
    queue.append(data[0])

    try:
        while len(queue) != 0:
            b = queue.popleft()
            if len(a[b]) != 0:
                for k in a[b]:
                    if memo[k] == 0:
                        memo[k] = 1
                        queue.append(k)
                        if k == data[1]:
                            print("yes")
                            raise Exception
        print("no")
    except Exception:
        pass

Python: #coding:utf-8

def CC(root,dist):
    for i in range(len(dist)):
        if i==root[0]:
            dist[i]=dist[i]+[root[1]]
        elif i==root[1]:
            dist[i]=dist[i]+[root[0]]
        
def Check(dist,start,end,clist):
    ans="no"
    if len(dist[end])!=0:
        if start in dist[end]:
            ans="yes"
        else:
            clist.append(end)
            for d in dist[end]:
                if d!=end and d not in clist:
                    ans=Check(dist,start,d,clist)
    return ans
    
if __name__=="__main__":
    I=input()
    n=int(I.split(" ")[0])
    m=int(I.split(" ")[1])
    A=[]
    for i in range(m):
        A.append(list(map(int,input().split(" "))))
    dist=[[]]*n
    for i in range(m):
        CC(A[i],dist)
    q=int(input())
    for i in range(q):
        qList=list(map(int,input().split(" ")))
        print(Check(dist,qList[0],qList[1],[]))

Python: def bfs(graph, start, goal):
    visited = {start: None}
    unvisited = [start]
    while unvisited:
        now = unvisited[0]
        if now == goal:
            return "yes"
        unvisited = unvisited[1:]
        for next in graph[now][1:]:
            if not (next in visited):
                unvisited.append(next)
                visited[next] = now
    return "no"

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

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

q = int(input())

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

Python: #coding:utf-8
n, r = map(int,input().split())
Q = []
M = [[] for j in range(n)]
for i in range(r):
    i,j = map(int,input().split())
    M[i].append(j)
    M[j].append(i)


def search(person, target):
    Q = []
    Q.append(person)
    color = [0 for i in range(n)]
    while Q != []:
        u = Q.pop(0)
        if u ==  target:
            print("yes")
            break
        color[u] = 1
        data = M[u]
        for v in data:
            if color[v] == 0:
                color[v] = 1
                Q.append(v)
        color[u] = 2
    else:
        print("no")

num = int(input())
for i in range(num):
    person, target = map(int,input().split())
    search(person, target)

Python: class ConnectedComponents():
    def __init__(self, n):
        self.size = [1] * n
        self.id = [i for i in range(n + 1)]

    def root(self, i):
        while i != self.id[i]:
            self.id[i] = self.id[self.id[i]]
            i = self.id[i]

        return i

    def relations(self, s, t):
        s_root = self.root(s)
        t_root = self.root(t)

        if s_root == t_root: 
            return

        if self.size[s_root] < self.size[t_root]:
            self.id[s_root] = t_root
            self.size[t_root] += self.size[s_root]
        else:
            self.id[t_root] = s_root
            self.size[s_root] += self.size[t_root]

    def find(self, s, t):
        return self.root(s) == self.root(t)

# INPUT
n, m = map(int, input().split())

CC = ConnectedComponents(n)

for _ in range(m):

    s, t = map(int, input().split())

    CC.relations(s, t)

# QUESTION
q = int(input())

for _ in range(q):

    s, t = map(int, input().split())

    if CC.find(s, t):
        print("yes")
    else:
        print("no")

Python: from collections import defaultdict


n, m = map(int, input().split())
rel = defaultdict(set)

for _ in range(m):
    p, r = map(int, input().split())
    rel[p].add(r)
    rel[r].add(p)


color = defaultdict(int)

def search(f, c):
    stack = [f]
    color[f] = c
    while stack:
        u = stack.pop()
        for v in rel[u]:
            if not color[v]:
                color[v] = c
                stack.append(v)


# assign color
cl = 0
for i in range(n):
    if not color[i]:
        cl += 1
        search(i, cl)

qn = int(input())

for _ in range(qn):
    f, l = map(int, input().split())
    print(['no', 'yes'][color[f] == color[l]])

Python: import sys
sys.setrecursionlimit(200000)

def dfs(v):
    global node_chk
    global B
    global connect
    global value

    node_chk[v] = 1
    connect[v] = value
    for x in B[v]:
        if(node_chk[x] == 0):
                dfs(x)



n,m = map(int,input().split())
B = [[]for i in range(n)] # 隣接リスト
node_chk = [0 for i in range(n)]
connect = [0 for i in range(n)]
value = 0

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

for i in range(n):
    if(node_chk[i] == 1):
        continue
    dfs(i)
    value += 1


q = int(input())
for i in range(q):
    start,target = map(int,input().split())
    if(connect[start] == connect[target]):
        print("yes")
    else:
        print("no")