Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 10000;

vector<int> G[MAX];
int T[MAX];
int A[N], B[N];

int find_next(int i, int n) {
	int ret = -1;
	for (int j = i; j < n; j++) {
		if (T[j]) continue;
		ret = j;
		break;
	}
	return ret;
}

int main() {
	int n, m; cin >> n >> m;
	rep(i, m) {
		int s, t; cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
		T[i] = 0;
	}
	int q; cin >> q;
	rep(i, q) {
		int a, b; cin >> a >> b;
		A[i] = a; B[i] = b;
	}

	queue<int> Q;
	int next = 0, type = 0;
	while (true) {
		if (Q.empty()) {
			int i = find_next(next, n);
			if (i == -1) break;
			type++;
			T[i] = type;
			Q.push(i);
			continue;
		}
		int i = Q.front(); Q.pop();
		for (auto j : G[i]) {
			if (T[j]) continue;
			T[j] = type;
			Q.push(j);
		}
	}
	rep(i, q) {
		if (T[A[i]] == T[B[i]]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

C++: #include <iostream>
#include <climits>
#include <stack>
#include <vector>

using namespace std;
static const int MAX=100000;
static const int NIL=-1;
int n;
vector <int> G[MAX];
int color[MAX];

void dfs(int r,int c)
{
    stack<int> S;
    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]==NIL)
            {
                color[v]=c;
                S.push(v);
            }
        }
    }
}

void assign_color()
{
    int id=1;
    for(int i=0;i<n;i++) color[i]=NIL;
    for(int u=0;u<n;u++)
    {
        if(color[u]==NIL) dfs(u,id++);
    }
}

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

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

C++: #include <iostream>
#include <stdint.h>
#include <vector>

using namespace std;

#define GRAPH_MAX_SIZE 100000
#define WHITE -1

vector<uint32_t> vertexes[GRAPH_MAX_SIZE];
int32_t colors[GRAPH_MAX_SIZE];

void graph_connected_components_init()
{
  uint32_t i;
  for (i = 0; i < GRAPH_MAX_SIZE; i++) {
    vertexes[i].clear();
    colors[i] = WHITE;
  }
}

void graph_connected_components_add(uint32_t vertex, uint32_t connect_to)
{
  vertexes[vertex].push_back(connect_to);
}

uint32_t *graph_connected_components_connected_vertexes(uint32_t vertex)
{
  return &vertexes[vertex][0];
}

void colorize_connecting_vertex(vector<uint32_t> *connect_to, int32_t color)
{
  uint32_t i;
  for (i = 0; i < connect_to->size(); i++) {
    uint32_t index = (*connect_to)[i];
    if (colors[index] == WHITE) {
      colors[index] = color;
      colorize_connecting_vertex(&vertexes[index], color);
    }
  }
}

void graph_connected_components_colorize()
{
  int32_t i;
  for (i = 0; i < 20; i++) {
    if (colors[i] == WHITE) {
      colors[i] = i;
      vector<uint32_t> connect_to = vertexes[i];
      colorize_connecting_vertex(&connect_to, i);
    }
  }
}

bool graph_connected_components_is_connected(uint32_t index1, uint32_t index2)
{
  if (colors[index1] == colors[index2]) return true;
  return false;
}

#if !defined(TEST)
#include <iostream>
using namespace std;
int32_t main(void)
{
  graph_connected_components_init();

  int32_t users, connections;
  cin >> users >> connections;

  uint32_t i;
  for (i = 0; i < connections; i++) {
    uint32_t index, connection;
    cin >> index >> connection;
    graph_connected_components_add(index, connection);
  }
  graph_connected_components_colorize();

  int32_t questions;
  cin >> questions;
  for (i = 0; i < questions; i++) {
    uint32_t index1, index2;
    cin >> index1 >> index2;
    if (graph_connected_components_is_connected(index1, index2)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}
#endif

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];


void dfs(int r, int c) {
	stack<int> S;
	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] == NIL) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor() {
	int id = 1;
	for (int i = 0; i < n; i++)color[i] = NIL;
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)dfs(u, id++);
	}
}

int main()
{
	int s, t, m, q;
    
	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G.push_back(t);
		G.push_back(s);
	}

	assignColor();

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

C++: #include<iostream>
#define N 100001
using namespace std;

int father[N];
int findfather(int i){
    while(father[i]!=i){
        i=father[i];
    }
    return i;
}
void changeGroup(int a,int b){
    // merge b t a
    int fa=findfather(a);
    int fb=findfather(b);
    father[fb]=fa;
}

int main(){
    int n,m,q;
    int a,b;
    cin>>n>>m;
    for(int i=0;i<n;i++) father[i]=i;
    for(int i=0;i<m;i++){
        cin>>a>>b;
        changeGroup(a,b);
    }
    cin>>q;
    for(int i=0;i<q;i++){
        cin>>a>>b;
        if(findfather(a)==findfather(b)){
            cout<<"yes"<<endl;
        }else
            cout<<"no"<<endl;
    }
    return 0;
}