Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: #include <cstdio>
#include <set>
#include <queue>
#include <vector>

bool isConnected(int id_from, int id_to, std::vector<std::set<int>> firend_relationships)
{
  std::queue<int> next_queue;
  std::set<int> searched_set;
  next_queue.push(id_from);

  while (!next_queue.empty())
  {
    int current_id = next_queue.front();
    next_queue.pop();
    searched_set.insert(current_id);

    std::set<int>::const_iterator itr;
    for (itr = firend_relationships[current_id].begin();
         itr != firend_relationships[current_id].end();
         ++itr)
    {
      // Ignore searched ones
      if (searched_set.count(*itr) != 0)
        continue;

      // Found
      if (*itr == id_to)
        return true;

      next_queue.push(*itr);
    }
  }

  // Not found
  return false;
}

int main()
{
  int n, m;
  scanf("%d%d", &n, &m);

  std::vector<std::set<int>> firend_relationships;
  firend_relationships.resize(n);
  for (int i = 0; i < m; ++i)
  {
    int id1, id2;
    scanf("%d%d", &id1, &id2);
    firend_relationships[id1].insert(id2);
    firend_relationships[id2].insert(id1);
  }

  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; ++i)
  {
    int id1, id2;
    scanf("%d%d", &id1, &id2);
    if (isConnected(id1, id2, firend_relationships))
      printf("yes\n");
    else
      printf("no\n");
  }

  return 0;
}

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;
}