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: n, m = map(int, input().split())
rootList = [-1 for i in range(n)]
 
def getRoot(x):
    r = rootList[x]
    if r < 0:
        rootList[x] = x
    elif r != x:
        rootList[x] = getRoot(r)
    return rootList[x]
 
 
def setSameRoot(x, y):
    x = getRoot(x)
    y = getRoot(y)
    if x != y:
        rootList[x] = rootList[y] = min(x, y)
 
for i in range(m):
    s, t = map(int, input().split())
    setSameRoot(s, t)
q = int(input())
for i in range(q):
    s, t = map(int, input().split())
    print("yes" if getRoot(s) == getRoot(t) else "no")

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

g = {}
for i in range(m):
    s,t = map(int,input().split())
    val = g.get(s,[])
    val.append(t)
    g[s] = val
    val = g.get(t,[])
    val.append(s)
    g[t] = val

count = 0
def dfs(g, s, done=[]):
    global count
    count += 1
    if s in done:
        return
    done.append(s)
    for c in g[s]:
        dfs(g, c, done)
    return done

q = int(input())

n = set(g.keys())
cc = []
from sys import setrecursionlimit
setrecursionlimit(1500)
while n != set([]):
    t = n.pop()
    d = dfs(g, t, [])
    cc.append(d)
    n = n - set(d)

for j in range(q):
    s,t = map(int,input().split())
    k = True
    for c in cc:
        if s in c and t in c:
            print('yes')
            k = False
            break
    if k: print('no')

Python: from enum import Enum
import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001


V,E = map(int,input().split())

boss = [i for i in range(V)]
height = [0 for i in range(V)]

def get_boss(index):
    if index == boss[index]:
        return index
    else:
        boss[index] = get_boss(boss[index])
        return boss[index]


def is_same_group(a,b):
    return get_boss(a) == get_boss(b)

def unite(x,y):
    boss_x = get_boss(x)
    boss_y = get_boss(y)

    if boss_x == boss_y:
        return

    if height[x] > height[y]:

        boss[boss_y] = boss_x

    elif height[x] < height[y]:

        boss[boss_x] = boss_y

    else:
        boss[boss_y] = boss_x
        height[x] += 1


for loop in range(E):
    A,B = map(int,input().split())
    unite(A,B)

num_query = int(input())

for loop in range(num_query):
    A,B = map(int,input().split())
    if is_same_group(A,B) == True:
        print("yes")
    else:
        print("no")

Python: #!/usr/bin/env python3

import array
import collections


UNDEF = -1


def compute_connected_components(max_v, adj_list):
    group = array.array("l", (UNDEF for _ in range(max_v)))
    g_idx = 0
    for u in range(max_v):
        if group[u] == UNDEF:
            group[u] = g_idx
            q = collections.deque()
            q.append(u)
            while q:
                v = q.popleft()
                for w in adj_list[v]:
                    if group[w] == UNDEF:
                        group[w] = g_idx
                        q.append(w)
            else:
                g_idx += 1
    return group


def main():
    n, m = map(int, input().split())
    adj_list = [set() for _ in range(n)]
    for _ in range(m):
        s, t = map(int, input().split())
        adj_list[s].add(t)
        adj_list[t].add(s)
    group = compute_connected_components(n, adj_list)
    q = int(input())
    for _ in range(q):
        s, t = map(int, input().split())
        print("yes" if group[s] == group[t] else "no")


if __name__ == '__main__':
    main()

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


class LinkedList:
    def __init__(self, n):
        self.n = n
        self.ll = [[] for _ in range(self.n)]

    def link(self, u, v, direction=False):
        self.ll[u].append(v)
        if not direction:
            self.ll[v].append(u)

    def dfs(self):
        self.cc = [None] * self.n

        color = 1
        for u in range(self.n):
            if not self.cc[u]:
                self.dfs_visit(u, color)
                color += 1

    def dfs_visit(self, u, color):
        self.cc[u] = color
        for v in self.ll[u]:
            if not self.cc[v]:
                self.dfs_visit(v, color)

    def check(self, u, v):
        if self.cc[u] == self.cc[v]:
            print('yes')
        else:
            print('no')


if __name__ == '__main__':
    n, m = map(int, input().split())
    ll = LinkedList(n)

    for _ in range(m):
        u, v = map(int, input().split())
        ll.link(u, v)
    ll.dfs()

    q = int(input())
    for _ in range(q):
        u, v = map(int, input().split())
        ll.check(u, v)

Python: import sys

def Grouping(F):
    n = len(F)
    G = [None for i in range(n)]
    for u in range(n):
        if G[u] is None:
            G[u] = u
            S = [u]
            while(len(S) > 0):
                u1 = S.pop()
                for u2 in F[u1]:
                    if G[u2] is None:
                        G[u2] = u
                        S.append(u2)
    return G

def main():
    istr = sys.stdin.read()
    ilist = istr.splitlines()
    unum, fnum = list(map(int,ilist[0].split()))

    F = [[]  for i in range(unum)]
    fnum += 1
    for n in range(1, fnum):
        uid, fid = map(int,ilist[n].split())
        F[uid].append(fid)
        F[fid].append(uid)
    
    G = Grouping(F)

    qnum = int(ilist[fnum])
    fnum += 1
    for q in range(fnum, (fnum + qnum)):
        id1, id2 = map(int,ilist[q].split())
        if G[id1] == G[id2]:
            print("yes")
        else:
            print("no")

if __name__ == '__main__':
    main()

Python: def MAIN():
    n, m = map(int, input().split())
    li = [i for i in range(n)]
    def f(a):
        if li[a] == a:
            return a
        b = f(li[a])
        li[a] = b
        return b
    def solve(a, b):
        if f(a) == f(b):
            return "yes"
        return "no"
    for _ in range(m):
        x, y = map(int, input().split())
        if li[x] > li[y]:
            li[x] = li[y]
        else:
            li[y] = li[x]
    for _ in range(int(input())):
        x, y = map(int, input().split())
        print(solve(x, y))
MAIN()