Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func newGraph(nNodes int) *Graph {
	g := Graph{}
	g.nodes = make([]*GraphNode, nNodes)
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode] = &GraphNode{}
		g.nodes[iNode].key = GraphNodeKey(iNode)
	}
	return &g
}

var infty = 1000000000

type GraphNodeKey int

// GraphNode is a node for undirected graph
type GraphNode struct {
	key       GraphNodeKey
	adjacents []*GraphNode
	visited   bool
	d, f      int
	parent    *GraphNode
	cc        int
}

func (g *Graph) setAdjacent(l, r GraphNodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, g.nodes[r])
}

// Graph is a undirected graph
type Graph struct {
	nodes []*GraphNode
}

func (g *Graph) initialize() {
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode].visited = false
		g.nodes[iNode].d = infty
		g.nodes[iNode].f = infty
		g.nodes[iNode].parent = nil
		g.nodes[iNode].cc = -1 // 22.3-12
	}
}

type QueueEntry struct {
	node *GraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *GraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

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

var time = 0

func fillDistancesByDepthFirstSearch(g *Graph) {
	g.initialize()
	time = 0
	cc := 0

	for _, u := range g.nodes {
		if !u.visited {
			dfsVisit(g, u, cc)
			cc++
		}
	}
}

func dfsVisit(g *Graph, u *GraphNode, cc int) {
	time++
	u.d = time
	u.cc = cc

	u.visited = true
	for _, v := range u.adjacents {
		if !v.visited {
			v.parent = u
			dfsVisit(g, v, cc)
		}
	}
	time++
	u.f = time
}

func main() {
	scanner.Split(bufio.ScanWords)

	nUsers := nextInt()
	nEdges := nextInt()
	graph := newGraph(nUsers)
	for iEdge := 0; iEdge < nEdges; iEdge++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())
		graph.setAdjacent(l, r)
		graph.setAdjacent(r, l)
	}

	fillDistancesByDepthFirstSearch(graph)

	nQueries := nextInt()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())

		// fmt.Println(graph.nodes[l].d, graph.nodes[r].d, graph.nodes[r].f, graph.nodes[l].f)
		if graph.nodes[l].cc == graph.nodes[r].cc {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

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

#include <memory>
#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 <cstdio>
#include <cctype>
#include <vector>
using namespace std;

#define si static inline
#define gcu getchar_unlocked
si int in() {
	int c, n = 0;
	bool minus = false;
	if ((c = gcu()) == '-') minus = true; else ungetc(c, stdin);
	while ((c = gcu()) >= '0' && c <= '9') n = 10 * n + (c - '0');
	return minus ? -n : n; }
si void scan(char *s) {while (!isspace(*s++ = gcu()));}
#define pcu putchar_unlocked
#define svo si void out
template <typename T>
svo(T n) {
	static char buf[20];
	char *p = buf;
	if(n < 0) pcu('-'), n *= -1;
	if (!n) *p++ = '0';
	else while (n) *p++ = n % 10 + '0', n /= 10;
	while (p != buf) pcu(*--p); }
svo(const char *s){while(*s)pcu(*s++);}
svo(char *s){while(*s)pcu(*s++);}
svo(char c){pcu(c);}
template <typename T>
//svo(vector<T> &v){for(T &x:v)out(' '),out(x);out('\n');}
svo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);out('\n');}
template <typename head, typename... tail>
svo(head&& h, tail&&... t){out(h);out(move(t)...);}

typedef vector<int> v;
struct graph {
	struct node {v n; int gid = 0;};
	vector<node> nodes;
	int gid_counter = 1;

	graph(int n) {
		nodes.resize(n);
	}
	void read(int n) {
		while (n--) {
			int s = in(), t = in();
			nodes[s].n.push_back(t);
			nodes[t].n.push_back(s);
		}
	}
	void traverse(int id, int gid) {
		node &n = nodes[id];
		if (n.gid)
			return;
		n.gid = gid ? gid : gid_counter++;
		for (int i: n.n)
			traverse(i, n.gid);
	}
	bool find(int s, int t) {
		traverse(s, nodes[s].gid);
		return nodes[s].gid == nodes[t].gid;
	}
};

int main() {
	int n = in(), m = in();
	graph g(n);
	g.read(m);
	int q = in();
	while (q--) {
		int s = in();
		out(g.find(s, in())? "yes" : "no", '\n');
	}
}