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

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

G = [[] for i in range(n)]
check, dist = [0 for i in range(n)], [-1 for i in range(n)]


def bfs(s, e):
    for i in range(n):
        check[i], dist[i] = 0, -1
    q = deque()

    check[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(len(G[u])):
            if check[G[u][v]] == 0:
                check[G[u][v]], dist[G[u][v]] = 1, dist[u] + 1
                q.append(G[u][v])

        check[u] = 2

    return dist[e]


def main():
    for i in range(m):
        a, b = map(int, input().split())
        G[a].append(b)
        G[b].append(a)

    q = int(input())

    for i in range(q):
        s, t = map(int, input().split())
        k = bfs(s, t)
        if k == -1:
            print("no")
        else:
            print("yes")


if __name__ == '__main__':
    main()

Python: V, E = map(int, input().split())
edge = [[] for _ in range(V)]
for _ in range(E):
    s, t = map(int, input().split())
    edge[s].append(t)
    edge[t].append(s)

group = [-1] * V
cnt = 0
for i in range(V):
    if group[i] == -1:
        group[i] = cnt
        stack = [i]
        while stack:
            v = stack.pop()
            for c in edge[v]:
                if group[c] == -1:
                    group[c] = cnt
                    stack.append(c)
        cnt += 1
for _ in range(int(input())):
    s, t = map(int, input().split())
    print('yes' if group[s] == group[t] else 'no')

Python: def warshall_floyd(w):
    inf = 1E+100
    n = len(w)
    d = w
    for k in range(n):
        d_new = [[inf for _ in range(n)] for _ in range(n)]
        for i in range(n):
            for j in range(n):
                d_new[i][j] = min(d[i][j], d[i][k] + d[k][j])
        d = d_new
    return d


inf = 1E+100
s = [int(x) for x in input().split()]
n = s[0]
m = s[1]
w = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
    w[i][i] = 0
for _ in range(m):
    s = [int(x) for x in input().split()]
    w[s[0]][s[1]] = 1
    w[s[1]][s[0]] = 1

d = warshall_floyd(w)

q = int(input())
for _ in range(q):
    s = [int(x) for x in input().split()]
    u = s[0]
    v = s[1]
    if d[u][v] < inf:
        print("yes")
    else:
        print("no")

Python: # -*- coding: utf-8 -*-
from collections import deque

N, M = tuple(map(int, input().split()))
adj = [[] for n in range(N)]

for _ in range(M):
    inp = tuple(map(int, input().split()))
    adj[inp[0]].append(inp[1])
    adj[inp[1]].append(inp[0])

unsearched = set(range(N))
colors = {n:-1 for n in range(N)}
color = -1

for n in range(N):
    if colors[n] != -1:
        continue
    color += 1
    start = n
    
    que = deque([start])
    while len(que):
        q = que.pop()
        next_ = [i for i in adj[q] if colors[i] == -1]
        que.extendleft(next_)
        colors[q] = color
    
Q = int(input())

for i in range(Q):
    question = tuple(map(int, input().split()))
    if colors[question[0]] == colors[question[1]]:
        print("yes")        
    else:
        print("no")

Python: from collections import deque

V, E = map(int, input().split())
Edge = [[] for _ in range(V)]

for _ in range(E): #??£????????????
    u, v = map(int, input().split())
    Edge[u].append(v)
    Edge[v].append(u)
    
Group = [-1] * V #Group???????????????????????°?????????????±????????????????????????°?????????????????°??????????????????????????????
Cnt = 0 # Group???????????????????±????????????????

for i in range(V):
    if Group[i] == -1:
        Group[i] = Cnt
        Stack = deque([i])
        while Stack:
            v = Stack.pop()
            for c in Edge[v]:
                if Group[c] == -1:
                    Group[c] = Cnt
                    Stack.append(c)
        Cnt += 1

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

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):
    data = M[person]
    for per in data:
        if per ==  target:
            global yes
            yes = 1
            print("yes")
            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")

Python: def dfs(r, c, g, color):
    s = [r]
    color[r] = c
    while len(s) != 0:
        u = s.pop()
        for i in range(len(g[u])):
            v = g[u][i]
            if color[v] is None:
                color[v] = c
                s.append(v)


def assignColor(n, g, color):
    color_id = 1
    for u in range(n):
        if color[u] is None:
            dfs(u, color_id, g, color)
            color_id += 1


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

color = [None] * n
assignColor(n, g, color)

q = int(input())

for i in range(q):
    s, t = [int(v) for v in input().split()]
    if color[s] == color[t]:
        print('yes')
    else:
        print('no')