Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

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


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

C++: #include <stdio.h>
#include <stdint.h>
#include <iostream>
#include <fstream>
#include <cassert>
#include <climits>

#include <cstring>
#include <queue>
#include <map>
#include <utility>
using namespace std;

#define DEBUG (0)

#if DEBUG
#define DPRINTF(...) do { \
    printf("D(L%d) %s: ", __LINE__, __func__); \
    printf(__VA_ARGS__); \
  } while (false)
#else
#define DPRINTF(...)
#endif // DEBUG

/*
   Memory limitaion; 64 MiB
   # of users: 2 <= n <= 100 K
   # of relations: 0 <= m <= 100 K

   Data size:
    1) for relations (maybe hash table):
      100K * 4Byte * 2 (bi-direction) = 800K

    2) for visited flag:
      100K * 1Byte = 100K
*/

const int kMaxUsers = 100 * 1000;

bool g_isVisited[kMaxUsers]; // 100 KiB
multimap<int, int> g_relations; // 800 KiB at least
multimap<int, int> g_reachable; // 800 KiB at least


void setRelations(int id0, int id1)
{
  g_relations.insert(make_pair(id0, id1));
  g_relations.insert(make_pair(id1, id0));
}


void setReachable(int start, int end)
{
  g_reachable.insert(make_pair(start, end));
}


bool checkReachableFromStatic(int start, int end)
{
  g_reachable.insert(make_pair(start, end));

  std::multimap<int, int>::iterator it;
  for (it = g_reachable.equal_range(start).first; it != g_relations.equal_range(start).second; it++)
  {
    if (it->second == end)
      return true;
  }

  return false;
}


string isReachable(int num, int start, int end)
{

  // init
  memset(g_isVisited, 0, num);

  queue<int> que;
  que.push(start);

  while (!que.empty())
  {
    int id = que.front();

    if (checkReachableFromStatic(id, end))
      return string("yes");

    que.pop();
    g_isVisited[id] = true;

    // if no path from "id", try next
    if (g_relations.count(id) == 0)
      continue;

    // check all pathes connected from "id"
    std::multimap<int, int>::iterator it;
    for (it = g_relations.equal_range(id).first; it != g_relations.equal_range(id).second; it++)
    {
      int &dst = it->second;

      if (dst == end)
      {
        setReachable(start, end);
        return string("yes");
      }

      if (!g_isVisited[dst])
        que.push(dst);

    }
  }

  return string("no");
}


void printAllRelations()
{
#if DEBUG
  for (auto& m: g_relations)
    DPRINTF("%d %d\n", m.first, m.second);
#endif // DEBUG
}


int main()
{
  int n, m;
  cin >> n >> m; cin.ignore();

  DPRINTF("number of users %d\n", n);
  DPRINTF("number of relations %d\n", m);

  for (int i = 0; i < m; i++)
  {
    int s, t;
    cin >> s >> t; cin.ignore();

    setRelations(s, t);
  }

  printAllRelations();

  int q;
  cin >> q; cin.ignore();

  for (int i = 0; i < q; i++)
  {
    int s, t;
    cin >> s >> t; cin.ignore();

    DPRINTF("src %d  dst %d\n", s, t);

    cout << isReachable(n, s, t) << endl;
  }

  return 0;

}

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

bool dfs(std::vector<std::vector<int> > n, int from, int to) {
	std::stack<int> s;
	std::vector<bool> went(n.size(), false);

	s.push(from);
	while (s.size() != 0){
		int num = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
			if (*it == to) return true;
			if (!went[*it]) {
				s.push(*it);
				went[*it] = true;
			}
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (dfs(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

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

using namespace std;

#define MAX 10000

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

void dfs(int r,int c)
{

	stack<int> S;
	S.push(r);
	color[r]=c;
	while(!S.empty())
	{
		int u=S.top();
		S.pop();
		for (int i = 0; i < G[u].size(); ++i)
		{
			int v=G[u][i];
			if (color[v]==-1)
			{
				color[v]=c;
				S.push(v);
			}
		}
	}
}

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

int main(int argc, char const *argv[])
{
	int s,t,m,q;
	cin>>n>>m;

	for (int i = 0; i < m; ++i)
	{
		cin>>s>>t;
		G[s].push_back(t);
		G[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;
}