Go: // AOJ ALDS1_11_D: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_11_D

package main

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

var nocolor = -1

type graph struct {
	graph  [][]int
	colors []int
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

func newGraph(nodeNum int) *graph {
	colors := make([]int, nodeNum)
	g := make([][]int, nodeNum)
	for i := 0; i < nodeNum; i++ {
		g[i] = []int{}
		colors[i] = nocolor
	}

	return &graph{g, colors}
}

func constructGraph(sc *bufio.Scanner) *graph {
	usersNum := nextInt(sc)
	linkNum := nextInt(sc)

	g := newGraph(usersNum)
	for i := 0; i < linkNum; i++ {
		u := nextInt(sc) // node
		l := nextInt(sc) // link node
		// edge each other
		g.graph[u] = append(g.graph[u], l)
		g.graph[l] = append(g.graph[l], u)
	}
	return g
}

func (g *graph) dfs(r, color int) {
	g.colors[r] = color

	stack := list.New()
	stack.PushFront(r)

	for stack.Len() > 0 {
		v := stack.Remove(stack.Front()).(int)
		for _, u := range g.graph[v] {
			if g.colors[u] == nocolor {
				g.colors[u] = color
				stack.PushFront(u)
			}
		}
	}
}

func (g *graph) assignColor() {
	color := 1
	for u := 0; u < len(g.graph); u++ {
		if g.colors[u] == nocolor {
			g.dfs(u, color)
			color++
		}
	}
}

func (g *graph) isLink(s, t int) bool {
	return g.colors[s] == g.colors[t]
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	// construct graph and friendly colors
	g := constructGraph(sc)
	g.assignColor()

	// judge link
	q := nextInt(sc)
	for i := 0; i < q; i++ {
		s := nextInt(sc)
		t := nextInt(sc)

		if g.isLink(s, t) {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

Python: import sys

def srch(id1, id2, D, L):
    cnt = 0

    S = [id1]
    while(len(S) > 0):
        u = S.pop()
        #print("cnt:{} id2:{} u:{} D:{} S:{}".format(cnt, id2, u, D[u], S))
        if L[u] == -1:
            L[u] = cnt
            cnt += 1
            if u == id2:
                return
            if D[u] is not None:
                A = D[u][:]
                while(len(A)):
                    u1 = A.pop()
                    if L[u1] == -1:
                        S.append(u1)
    

def main():
    istr = sys.stdin.read()
    ilist = []
    for i in istr.splitlines():
        ilist.append(list(map(int,i.split())))
    #print(ilist)
    unum, fnum = ilist[0]

    Dst = [[] for i in range(unum)]
    fnum += 1
    for n in range(1, fnum):
        uid, fid = ilist[n]
        Dst[uid].append(fid)
        Dst[fid].append(uid)
    
    qnum = ilist[fnum][0]
    fnum += 1
    #print("fnum={} qnum={}".format(fnum,qnum))
    for q in range(fnum, (fnum + qnum)):
        id1, id2 = ilist[q]
        F = [-1] * (unum)
        #print("\n[{}] id1={:2} id2={:2} D:{}".format(q,id1, id2, Dst))
        srch(id1, id2, Dst, F)
        if F[id2] != -1:
            print("yes")
        else:
            print("no")

if __name__ == '__main__':
    main()

Python: import sys
sys.setrecursionlimit(1000000)
n, m = map(int, input().split())
graph = [[] for _ in range(n)]

global visited 
visited = [0 for _ in range(n)]
global Q, S, T

S = [0] #訪れた順
T = []
visited[0] = 1 #開始位置はすでに訪れたことにする

def input_graph(m):
  for _ in range(m):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

def dfs():
  global Q, S
  while Q:
    node_id = Q.pop()

    for ad_node in graph[node_id]:
      if visited[ad_node] == 1:
        continue
      visited[ad_node] = 1
      S.append(ad_node)
      Q.append(ad_node)


"""
def dfs(node_id):
  
  for ad_node in graph[node_id]:
    if visited[ad_node] == 1:
      continue
    visited[ad_node] = 1
    dfs(ad_node)
"""
if __name__ == "__main__":
  input_graph(m)
  q_num = int(input())
  for _ in range(q_num):
    visited = [0 for _ in range(n)]
    s, t = map(int, input().split())
    Q = deque([s])
    dfs()
    if visited[t]:
      print("yes")
    else:
      print("no")

Python: n, m = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(m):
    s = [int(x) for x in input().split()]
    adj[s[0]].append(s[1])
    adj[s[1]].append(s[0])

cc = [-1 for _ in range(n)]
cnt = 0
for i in range(n):
    if cc[i] < 0:
        cc[i] = cnt
        stack = [i]
        while stack:
            v = stack.pop()
            for u in adj[v]:
                if cc[u] < 0:
                    cc[u] = cnt
                    stack.append(u)
        cnt += 1

q = int(input())
for _ in range(q):
    s, t = map(int, input().split())
    if cc[s] == cc[t]:
        print("yes")
    else:
        print("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
    visited = [0] * n
    queue = deque()
    queue.append(s)

    while queue:
        i = queue.popleft()
        if i == t:
            return True
        for f in g[i]:
            if not visited[f]:
                visited[f] = 1
                queue.append(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: import sys
input = lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**6)
def dfs(g,v,z):
    if vis[v]!=0: return
    else:
        vis[v]=z
        for p in g[v]:
            dfs(g,p,z)
n,m=map(int,input().split())
g=[[] for i in range(n)]
for i in range(m):
    x,y=map(int,input().split())
    g[x].append(y)
    g[y].append(x)
z=0 ; vis=[0]*n
for i in range(n):
    if vis[i]==0:
        z+=1
        dfs(g,i,z)
q=int(input())
for i in range(q):
    x,y=map(int,input().split())
    if vis[x]==vis[y]: print('yes')
    else: print('no')

Python: #coding:utf-8
import sys
sys.setrecursionlimit(10000)
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):
    data = M[person]
    for per in data:
        if per ==  target:
            global yes
            yes = 1
            return
        if color[per] == "white":
            color[per] = "gray"
            search(per, target)
    color[person] = "black"

num = int(input())
for i in range(num):
    yes = 0
    color = ["white" for j in range(n)]
    person, target = map(int,input().split())
    search(person, target)
    if yes == 0:
        print("no")
    else:
        print("yes")

Python: n, m = map(int, input().split())
g = [set() for _ in range(n)]
group_ids = [0] * n

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


def search(s, gid):
    global g, group_ids
    queue = set(g[s])
    group_ids[s] = gid

    while queue:
        i = queue.pop()
        if not group_ids[i]:
            group_ids[i] = gid
            queue.update(g[i])


gid = 1
for i in range(n):
    if group_ids[i]:
        continue
    search(i, gid)
    gid += 1

q = int(input())
while q:
    s, t = map(int, input().split())
    gs, gt = group_ids[s], group_ids[t]
    print('yes' if gs and gt and gs == gt else 'no')
    q -= 1

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 = ["white" for i in range(n)]
    while Q != []:
        u = Q.pop(0)
        if u ==  target:
            print("yes")
            break
        color[u] = "gray"
        data = M[u]
        for v in data:
            if color[v] == "white":
                color[v] = "gray"
                Q.append(v)
        color[u] = "black"
    else:
        print("no")

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