Go: package main

import (
	"fmt"
	"bufio"
	"os"
	"strconv"
)

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

C++: #include <bits/stdc++.h>
using namespace std;

int n, m;

bool frend(vector<vector<int>> &M, vector<int> &memo, int i, int t) {
  if (memo[i] == 1) return false;
  for (int z = 0; z < M[i].size(); z++) {
    if (M[i][z] == t) return true;
    memo[i] = 1;
    if (frend(M, memo, M[i][z], t)) { return true; }
  }
  return false;
}

int main() {
  cin >> n >> m;
  vector<vector<int>> M(n);

  for (int i = 0; i < m; i++) {
    int id, f;
    cin >> id >> f;
    M[id].push_back(f);
    M[f].push_back(id);
  }
  int qn;
  cin >> qn;
  vector<vector<int>> q(qn, vector<int>(2));
  for (int i = 0; i < qn; i++) {
    cin >> q[i][0] >> q[i][1];
  }

  for (int i = 0; i < qn; i++) {
    vector<int> memo(n);
    bool res = frend(M, memo, q[i][0], q[i][1]);
    if (res) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
}

C++: #include<iostream>
#include<vector>

using namespace std;

int main(){
	int n, m, s, t, q, i, j;
	int id;
	bool answer_yes = false;

	cin >> n >> m;
	vector< vector<int> > Adj(n);
	vector<int> Stack;
	vector<int> Check(n);
	vector<int>::iterator it, itend;

	for (i = 0; i < m; i++) {
		cin >> s >> t;
		Adj[s].push_back(t);
		Adj[t].push_back(s);
	}

	/*for (i = 0; i < n; i++) {
		cout << "\n" << i << " : " << flush;
		for (auto it2 = Adj[i].begin(); it2 != Adj[i].end(); it2++) {
			cout << *it2 << " " << flush;
		}
	}
	cout << endl;*/

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> s >> t;
		
		// calculate
		Stack.push_back(s);
		Check[s] = true;
		while (!Stack.empty()) {
			id = Stack.back();
			//cout << "\nid : " << id << " stc : " << Stack.size() << endl;
			itend = Adj[id].end();
			for (it = Adj[id].begin(); it != itend; it++) {
				//cout << *it << " " << flush;
				if (!Check[*it]) {
					if (*it == t) {
						answer_yes = true;
						Stack.clear();
						break;
					}
					Stack.push_back(*it);
					Check[*it] = true;
					break;
				}
			}
			if (it == itend) {
				Stack.pop_back();
			}
		}
		if (answer_yes) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
			Stack.clear();
		}
		answer_yes = false;
		itend = Check.end();
		for (it = Check.begin(); it != itend; it++) {
			*it = false;
		}
	}
	
	return 0;
}

C++: 

#include<iostream>
#include<vector>
#include<stack>

using namespace std;


static const int MAX = 100'000;
static const int nil = -1;

int n;
vector<int> grp[MAX];
int color[MAX];



void dfs(int r, int c) {
	stack<int> stk;
	stk.push(r);
	color[r] = c;

	while (!stk.empty())
	{
		int u = stk.top();stk.pop();

;		for (int i = 0; i < grp[u].size(); i++)
		{
			int v = grp[u][i];
			if (color[v] == nil) {
				color[v] = c;
				stk.push(v);
			}
		}
	}
}

void assignColor() {
	int id = 1;
	for (int i = 0; i < n; i++) color[i] = nil;
	for (int u = 0; u < n; u++)
	{
		if (color[u] == nil) dfs(u, id++);
	}
}

int main() {



	int s, t, m, q;

	cin >> n >> m;

	for (int i = 0;i < m;i++) {
		cin >> s >> t;
		grp[s].push_back(t);
		grp[t].push_back(s);
	}

	assignColor();

	cin >> q;
	for (int i = 0; i < q; i++)
	{
		cin >> s >> t;
		if (color[s] == color[t]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}


	return 0;
}

C++: 
import java.util.*;


public class Main{ 

	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		Graph graph = new Graph(scan.nextInt());
		int relationNum = scan.nextInt();
		for(int i=0; i<relationNum; i++)
			graph.setRelation(scan.nextInt(), scan.nextInt());
		int queryNum = scan.nextInt();
		for(int i=0; i<queryNum; i++)
			if(graph.bfs(scan.nextInt(), scan.nextInt())==-1)
				System.out.println("no");
			else
				System.out.println("yes");
		//graph.bfs(16);

	}

}
class Graph{
	int verNum;
	ArrayList<Node> vertices;
	public Graph(int n){
		vertices = new ArrayList<Node>();
		this.verNum = n;
		for(int i=0; i<verNum;i++)
			vertices.add(new Node(i));
	}
	public void setRelation(int a, int b){
		vertices.get(a).addNext(vertices.get(b));
	}
	public Graph(int[][] matrix){
		vertices = new ArrayList<Node>();
		for(int i=0; i<matrix.length;i++)
			vertices.add(new Node(i+1));
		for(int i=0; i<matrix.length;i++)
			for(int j=0; j<matrix.length; j++)
				if(matrix[i][j]!=0)
					vertices.get(i).addNext(vertices.get(j));
	}
	void walk(){
		int time = 0;
		for(int i=0; i<vertices.size();i++){
			time = vertices.get(i).walk(time);
		}
	}
	void nearest(){	
		//for(int i=0; i<vertices.size();i++)
			//System.out.println((i+1) + " " + bfs(i+1));
	}
	void clearHistory(){
		for(int i=0; i<vertices.size();i++)
					vertices.get(i).disSteped();	
	}
	void print(){

		for(int i=0; i<vertices.size();i++)
			vertices.get(i).result();
	}

	int bfs(int start, int target){
		ArrayDeque<Node> queue = new ArrayDeque<Node>();
		clearHistory();
		queue.addFirst(vertices.get(start));
		int dist = 0;
		int childCount = 1;
		int nextChildrenCount=0;
		while(!queue.isEmpty()){
			Node temp = queue.removeLast();
			if(temp.id==target)
				return dist;
			childCount--;

			for(Node n : temp.nextNode){
				if(!n.steped){
					n.steped = true;
					nextChildrenCount++;
					queue.addFirst(n);
				}
			}
			if(childCount==0){
				dist++;
				childCount = nextChildrenCount;
				nextChildrenCount=0;
			}
			//System.out.println("queue size : " + queue.size());
		}
		return -1;

	}
}

class Node{
	int id;
	int startTime;
	int endTime;
	boolean steped = false;
	ArrayList<Boolean> stepedNext = new ArrayList<Boolean>(); 
	ArrayList<Node> nextNode = new ArrayList<Node>();
	public Node(int id){
		this.id = id;
	}
	public void addNext(Node n){
		nextNode.add(n);
		stepedNext.add(false);
	}
	public void disSteped(){
		this.steped = false;
	}
	
	public int walk(int time){
		if(this.steped)
			return time;
		time++;//
		this.startTime = time;
		this.steped = true;
		int end = time;
		for(int i=0; i<stepedNext.size(); i++){
			time = nextNode.get(i).walk(time);
		}
		time++;//
		this.endTime = time;
		return endTime;
	}
	boolean remainNext(){
		return stepedNext.contains(false);
	}
	void result(){
		System.out.println(this.id + " " + startTime + " " + endTime);
	}
}
class AdjacencyMatrices{
	int pointNum;
	int[][] matrix;
	public AdjacencyMatrices(int n){
		pointNum = n;
		matrix = new int[n][];
		for(int i=0; i<n; i++)
			matrix[i] = new int[n];
	}
	public void readList(Scanner scan){
		for(int i=0; i<pointNum; i++){
			int index = scan.nextInt()-1;
			int dim = scan.nextInt();
			for(int j=0; j< dim; j++)
				matrix[index][scan.nextInt()-1] = 1;
		}
	}
	public int[][] getMatrix(){
		return matrix;
	}
	public void printMatrix(){
		StringBuilder temp = new StringBuilder();
		for(int i=0; i<pointNum; i++){
			for(int j=0; j<pointNum; j++){
				temp.append(matrix[i][j]);
				if(j!=pointNum-1)
					temp.append(" ");
			}
			temp.append("\n");
		}
		System.out.print(new String(temp));
	}

}