Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include<cstdio>

int par[100500];

int find(int x) {
    if(par[x] == x) return x;
    return par[x] = find(par[x]);
}

void unite(int x,int y) {
    x = find(x);
    y = find(y);
    par[x] = y;
}

void init() {
    for(int i=0; i<100500; i++)
        par[i] = i;
}

int main(void) {
    int n,m,q;
    scanf("%d%d",&n,&m);
    init();
    for(int i=0; i<m; i++) {
        int s,t;
        scanf("%d%d",&s,&t);
        unite(s,t);
    }
    scanf("%d",&q);
    for(int i=0; i<q; i++) {
        int s,t;
        scanf("%d%d",&s,&t);
        puts(find(s) == find(t) ? "yes" : "no");
    }
    return 0;
}

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

#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++: ///
// File:  alds1_11_d.cpp
// Author: ymiyamoto
//
// Created on Wed Oct 18 01:18:32 2017
//

#include <cstdint>
#include <iostream>
#include <vector>

using namespace std;

#define SIZE 100000
static vector<uint32_t> graph[SIZE];
static vector<int32_t> colors(SIZE, -1);

static void dfs(uint32_t index, uint32_t color)
{
  colors[index] = color;

  for (uint32_t i = 0; i < graph[index].size(); i++) {
    uint32_t to = graph[index][i];
    if (colors[to] == -1) {
      dfs(to, color);
    }
  }
}

int32_t main()
{
  uint32_t n, m;
  cin >> n >> m;

  for (uint32_t i = 0; i < m; i++) {
    uint32_t from, to;
    cin >> from >> to;
    graph[from].push_back(to);
    graph[to].push_back(from);
  }

  for (uint32_t i = 0; i < n; i++) {
    if (colors[i] == -1) {
      dfs(i, i);
    }
  }

  uint32_t q;
  cin >> q;

  for (uint32_t i = 0; i < q; i++) {
    uint32_t from, to;
    cin >> from >> to;
    if (colors[from] == colors[to]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}