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 <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
vector<int> g_list[kMaxUsers];


void setRelations(int start, int end)
{
  g_list[start].push_back(end);
  g_list[end].push_back(start);
}


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();

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

    for (int j = 0; j < g_list[id].size(); j++)
    {
      const int &next = g_list[id][j];

      if (next == end)
        return string("yes");

      if (!g_isVisited[next])
        que.push(next);
    }
  }

  return string("no");
}


void printAllRelations()
{
#if DEBUG
  for (int i = 0; i < kMaxUsers; i++)
  {
    for (int j = 0; j < g_list[i].size(); j++)
    {
      if (j == 0)
        DPRINTF("%d", i);

      printf(" %d", g_list[i][j]);

      if (j == g_list[i].size() - 1)
        cout << endl;
    }
  }
#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 <algorithm>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define maxn 10005
#define NTL -1
int color[maxn];
vector<int> g[maxn];
stack<int> s;
int n,m,q;
void tarjan(int r,int c)
{
    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]==NTL)
            {
                color[v]=c;
                s.push(v);
            }
        }
    }
}
void assigncolor()
{
    int id=0;
    for(int i=0;i<n;i++)
        color[i]=NTL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NTL)
            tarjan(u,id++);
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
    assigncolor();
    scanf("%d",&q);
    while(q>0)
    {
        q--;
        int u,v;
        scanf("%d%d",&u,&v);
        if(color[u]==color[v])
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <set>
#include <map>
using namespace std;

vector<vector<int> >g;
int n,m,q;

vector<bool>visited;
bool dfs(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    while(1){}
    cin>>n>>m;
    g.resize(n);
    
    for(int i=0;i<m;i++){
        int s,t;
        cin>>s>>t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
    cin>>q;
    for(int i=0;i<q;i++){
        visited.resize(n,false);
        int s,t;
        cin>>s>>t;
        cout<<(dfs(s,t)?"yes":"no")<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;

vector<int> numbers;

void setNum(vector<vector<int> > a){
    numbers.resize(a.size());
    for(int i = 0; i < numbers.size(); i++)numbers[i] = -1;
    int num = 0;
    queue<int> place;
    place.push(0);
    while(place.size()){
        int now = place.front();
        place.pop();
        numbers[now] = num;
        for(int i = 0; i < a[now].size(); i++){
            int next = a[now][i];
            if(numbers[next] == -1){
                place.push(next);
            }
        }
        if(place.size() == 0){
            for(int i = 0; i < numbers.size(); i++){
                if(numbers[i] == -1){
                    place.push(i);
                    num++;
                    break;
                }
            }
        }
    }
}

int main(void){
    int n, m;
    cin >> n >> m;
    vector<vector<int> > a(n);
    for(int i = 0; i < m; i++){
        int s, t;
        cin >> s >> t;
        a[s].push_back(t);
        a[t].push_back(s);
    }
    int q;
    cin >> q;
    setNum(a);
    for(int i = 0; i < q; i++){
        int s, t;
        cin >> s >> t;
        if(numbers[s] == numbers[t])cout << "yes" << endl;
        else cout << "no" << endl;
    }
}