Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t int
	G             [MAX][]int //連結リスト
	color         [MAX]int
)

func dfs(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

//幅優先探索で、頂点を色付け
func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			dfs(i, id)
		}
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)

	}
	assignColor()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

C++: #include <iostream>
#include <vector>
using namespace std;

class vertex{
public:
  vector<vertex*> to;
  bool visited;
  int num;
  int group;

  vertex(){
    visited=false;
  }

  void dfs(int group){
    visited=true;
    this->group = group;
    for(int i=0; i<to.size(); i++){
      if(!to[i]->visited) to[i]->dfs(group);
    }
  }
};

int main(){
  int n, m; cin >> n >> m;
  vertex graph[n];

  //隣接リストにグラフを格納
  int v, k, to;
  for(int i=0; i<m; i++){
    cin >> v >> to;
    graph[v].num = v;
    graph[v].to.push_back(&graph[to]);
    graph[to].num = to;
    graph[to].to.push_back(&graph[v]);
  }

  //頂点0からdfsを行い、グループ分け(各連結成分ごとに1から昇順に番号をつける)
  for(int v=0, group=1; v<n; v++){
    if(!graph[v].visited){
      graph[v].dfs(group);
      group++;
    }
  }

  //回答を得る
  int q, v1, v2; cin >> q;
  bool ans[q];
  for(int i=0; i<q; i++){
    cin >> v1 >> v2;
    if(graph[v1].group==graph[v2].group) ans[i]=true;
    else ans[i]=false;
  }

  //回答出力
  for(int i=0; i<q; i++){
    if(ans[i]) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}

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

#define N_MAX 100005
#define NIL -1

long n;
vector<long> G[N_MAX];
long color[N_MAX];

void bfs(long i, long id) {
    queue<long> S;
    S.push(i);
    color[i] = id;
    while (S.empty() == false) {
        long u = S.front();
        S.pop();
        for (long j = 0; j < G[u].size(); j++) {
            long v = G[u][j];
            if (color[v] == NIL) {
                color[v] = id;
                S.push(v);
            }
        }
    }
}

void assign_color(long i, long id) {
    bfs(i, id);
}

int main() {
    long m, q, s, t;
    cin >> n >> m;
    for (long i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    // for (long i = 0; i < n; i++) {
    //     cout << "Node " << i << ": ";
    //     for (long j = 0; j < G[i].size(); j++) {
    //         cout << " " << G[i][j];
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    for (long i = 0; i < n; i++) {
        color[i] = NIL;
    }

    long id = 0;
    for (long i = 0; i < n; i++) {
        if (color[i] == NIL) {
            assign_color(i, id);
            id++;
        }
    }

    // for (long i = 0; i < n; i++) {
    //     cout << " " << color[i];
    // }
    // cout << endl;

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

C++: //
//  main.cpp
//  connected_components
//
//  Created by ???????????? on 2017/10/22.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

#define n_max 100000
//#define vector_max 100000
#define WHITE 0
#define GRAY 1
#define BLACK 2

bool found;
int n, target, t = 0;
int color[n_max], d[n_max], f[n_max];
vector<int> G[n_max];

void encode_graph_list(){
    int i, id, node, f;
    
    scanf("%d", &n);
    scanf("%d", &node);
    
    for (i = 0; i < node; i++){
        scanf("%d", &id);
        scanf("%d", &f);
        G[id].push_back(f);
    }
    
}

void dfs(int u){
//    found = false;
//    if(target == u) {
//        found = true;
//        return;
//    }
    color[u] = GRAY;
    d[u] = ++t;
    for (int v = 0; v < G[u].size(); v++){
        int t = G[u][v];
        if(color[t] == WHITE){
            dfs(t);
        }
    }
    color[u] = BLACK;
    f[u] = ++t;
}

void dfs_init(int start){
    for(int i = 0; i < n ; i++){
        color[i] = WHITE;
    }
    dfs(start);
}



int main(int argc, const char * argv[]) {
    int qs, i, id;
    encode_graph_list();
    scanf("%d", &qs);
    for (i = 0; i < qs; i++){
        scanf("%d %d", &id, &target);
        dfs_init(id);
        if(color[target] == BLACK) cout << "yes" <<endl;
        else cout << "no"<<endl;
    }
//    std::cout << "Hello, World!\n";
    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
*/

multimap<int, int> g_relations;

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


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

  bool isVisited[num];

  for (int i = 0; i < num; i++)
    isVisited[i] = false;

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

  while (!que.empty())
  {
    int id = que.front();
    que.pop();
    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)
        return string("yes");

      if (!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;

}