Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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

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

using namespace std;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) { }
  bool unionSet(int x, int y) {
    x = root(x); y = root(y);
    if (x != y) {
      if (data[y] < data[x]) swap(x, y);
      data[x] += data[y]; data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) {
    return root(x) == root(y);
  }
  int root(int x) {
    return data[x] < 0 ? x : data[x] = root(data[x]);
  }
  int size(int x) {
    return -data[root(x)];
  }
};

int main() {
  int n, m, q;
  cin >> n >> m;
  UnionFind uf(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    uf.unionSet(s, t);
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    if (uf.findSet(s, t)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}