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 <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
ll mod = 1000000007;

const int MAX = 100000;
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();
        rep(i, G[u].size()) {
            int v = G[u][i];
            if(color[v] == NIL) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assignColor() {
    int id = 1;
    rep(i, n) {
        color[i] = NIL;
    }
    rep(u, n) {
        if(color[u] == NIL) {
            dfs(u, id++);
        }
    }
}
 
int main() {
    int s, t, m, q;

    cin >> n >> m;

    rep(i, m) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assignColor();

    cin >> q;

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

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

using namespace std;

#define GRAPH_SIZE 100000

enum state_t {
  WHITE,
  GRAY,
  BLACK,
};

struct vertex_t {
  uint32_t color;
  vector<uint32_t> connections;
};

static vertex_t graph[GRAPH_SIZE];
static state_t state[GRAPH_SIZE];

static void colorize(uint32_t index, uint32_t color)
{
  if (state[index] == WHITE) {
    state[index] = GRAY;
    graph[index].color = color;
    for (uint32_t i = 0; i < graph[i].connections.size(); i++) {
      colorize(graph[i].connections[i], color);
    }
    state[index] = BLACK;
  }
}

void check_connection()
{
  for (uint32_t i = 0; i < GRAPH_SIZE; i++) {
    state[i] = WHITE;
  }

  uint32_t color = 0;
  for (uint32_t i = 0; GRAPH_SIZE; i++) {
    colorize(i, color);
    color++;
  }
}

#include <iostream>

int32_t main()
{
  uint32_t n, m;
  cin >> n >> m;

  for (uint32_t i = 0; i < m; i++) {
    uint32_t index, to;
    cin >> index >> to;
    graph[index].connections.push_back(to);
  }

  check_connection();

  uint32_t qs;
  cin >> qs;

  for (uint32_t i = 0; i < qs; i++) {
    uint32_t from, to;
    cin >> from >> to;

    if (graph[from].color == graph[to].color) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}

C++: #include<stdio.h>
using namespace std;

const int MAX = 100001;

int parent(int table[], int child) {
	if (table[child] == 0)return child;
	return table[child] = parent(table, table[child]);
}

int main() {
	int n, m, q;
	int table[MAX] ={0};

	scanf("%d %d", &n, &m);

	for (int i = 0; i < m; i++) {
		int s, t;
		scanf("%d %d", &s, &t);
		s++;t++;
		int ps = parent(table, s);
		int pt = parent(table, t);
		if (ps != pt) {
			table[ps] = pt;
		}
	}

	scanf("%d", &q);

	for (int i = 0; i < q; i++) {
		int s, t;
		
		scanf("%d %d", &s, &t);
                s++;t++;
		int ps = parent(table, s);
		int pt = parent(table, t);

		if (ps == pt) {
			printf("yes\n");
		}
		else {
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_set>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< vector<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< vector<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).push_back(t);
		friendGraphList.at(t).push_back(s);
	}
	for (auto& v : friendGraphList)v.shrink_to_fit();
}

void accesibleNodeSearch(const vector<vector<int>>& graphList, vector<vector<int>>& visitMatrix, const int s) {
	const size_t n = graphList.size();
	queue<int> nodeQueue;
	vector<bool> visitList;
	visitList.resize(n, false);
	nodeQueue.push(s);
	visitList.at(s).flip();
	while (!nodeQueue.empty()) {
		const int id = nodeQueue.front();
		const vector<int>& adjList = graphList.at(id);
		for (const auto& adjID : adjList) {
			if (visitList.at(adjID))continue;
			const vector<int>& idList = graphList.at(adjID);
			if (find(idList.begin(), idList.end(), id) == idList.end())continue;
			nodeQueue.push(adjID);
			visitList.at(adjID).flip();
		}
		nodeQueue.pop();
	}
	const int num = count(visitList.begin(), visitList.end(), true);
	visitMatrix.at(s).reserve(num);
	for (int i = 0; i != n; ++i) {
		if (!visitList.at(i))continue;
		visitMatrix.at(s).push_back(i);
	}
}

void buildFriendAccesibility(const vector<vector<int>>& graphList, vector<vector<int>>& visitMatrix) {
	const int n = graphList.size();
	visitMatrix.resize(n, vector<int>());
	for (int i = 0; i != n; ++i) {
		accesibleNodeSearch(graphList, visitMatrix, i);
	}
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	vector<vector<int>> visitMatrix;
	buildFriendAccesibility(graphList, visitMatrix);
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		cout << ( find(visitMatrix.at(s).begin(), visitMatrix.at(s).end(),t) != visitMatrix.at(s).end() ? "yes" : "no") << endl;
	}
}

int main()
{


	execute(cin);

	return 0;
}

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

int N;
vector<int> par;
void init(int m)
{
    N = m;
    par.resize(N);
    for (int i = 0; i < N; ++i) par[i] = i;
}
int find(int x) {
    return (par[x] == x) ? x : (par[x] = find(par[x]));
}
int isunion(int x, int y) {
    return find(x) == find(y);
}
int tounion(int x, int y)
{
    int p = find(x), q = find(y);
    if (p == q) return 0;
    par[q] = p;
    return 1;
}

int main() {
    int n, m, q;
    cin >> n >> m;
    init(n);
    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        tounion(a, b);
    }
    cin >> q;
    for (int i = 0; i < q; ++i) {
        int a, b;
        cin >> a >> b;
        cout << (isunion(a, b) ? "yes\n" : "no\n");
    }
    return 0;
}