Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever while (true)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = acos(-1);
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
using namespace std;
// clang-format on

class UFTree {
  std::vector<int> data, rank, _size;

public:
  UFTree(const int &n) : data(n), rank(n), _size(n, 1) { iota(all(data), 0); }

  int root(const int &x) { return data[x] == x ? x : data[x] = root(data[x]); }

  bool same(const int &a, const int &b) { return root(a) == root(b); }

  void unite(int a, int b) {
    a = root(a), b = root(b);
    if (a == b) return;
    if (rank[a] < rank[b]) std::swap(a, b);
    data[b] = a;
    _size[a] += _size[b];
    if (rank[a] == rank[b]) rank[a]++;
  }

  int sizeOf(int x) { return _size[root(x)]; }
};

main {
  int n, m;
  cin >> n >> m;
  UFTree uf(n);
  while (m--) {
    int s, t;
    cin >> s >> t;
    uf.unite(s, t);
  }
  int q;
  cin >> q;
  while (q--) {
    int s, t;
    cin >> s >> t;
    cout << (uf.same(s, t) ? "yes" : "no") << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <unordered_map>
#include <stdio.h>
#include <list>
#include <vector>
#include <algorithm>
#include <stack>
#include <climits>
#include <queue>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; ++i)
typedef long long ll;



int main()
{
	int n;
	cin >> n;
	int m;
	cin >> m;
	vector <vector<int>> G(n);
	vector <int> color(n);

	rep(i,m)
	{
		int foo,fooo;
		cin >> foo >> fooo;
		//Sif(foo>fooo)swap(foo,fooo);
		G[foo].push_back(fooo);
		G[fooo].push_back(foo);
	}
	rep(i,n) color[i] = -1;
	queue<int> Q;
	rep(iii,n)
	{
		int v=-1;
		if(color[iii]==-1)v=iii;
		else continue;
		//cout<<v<<endl;
		color[v]=v;
		if(v!=-1)Q.push(v);
		while(!Q.empty())
		{
			int u = Q.front();
			color[u]=v;
			Q.pop();
			rep(i,G[u].size())
			{
				if(color[G[u][i]]==-1)Q.push(G[u][i]);
			}
		}
	}
	//rep(i,n)cout<<color[i]<<endl;
	int k;
	cin >> k;
	rep(i,k)
	{
		int foo,fooo;
		cin >> foo >> fooo;
		if(color[foo]==color[fooo])cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: // 隣接リストによる連結成分の判断
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#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 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[s].push_back(t);
        G[t].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 "bits/stdc++.h"
#include <unordered_set>

#define _CRT_SECURE_NO_WARNINGS

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

using namespace std;

const int INF = 1000000000;
const double PI = 3.1415926535897932384626433832795;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

using LL = long long int;
using LD = long double;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<LL, LL>;
using vi = vector<int>;
using vd = vector<double>;
using vll = vector<LL>;
using vs = vector<string>;
using vvi = vector<vi>;

//==============================================
int n, m, q;
vi color;
vvi G;

void antDfs(int r, int teamNum) {
	stack<int>S;
	S.push(r);
	color[r] = teamNum;
	while (!S.empty()) {
		int u = S.top(); S.pop();
		REP(i, G[u].size()) {
			int v = G[u][i];
			if (color[v] == -1) {
				color[v] = teamNum;
				S.push(v);
			}
		}
	}
}

void func() {
	int id = 1;
	REP(i, n) {
		if (color[i] == -1) {
			antDfs(i, id++);
		}
	}
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> n >> m;

	color.resize(n, -1);
	G.resize(n);

	int a, b;
	REP(i, m) {
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}

	func();


	cin >> q;

	REP(i, q) {
		cin >> a >> b;
		if (color[a] == color[b]) {
			cout << "yes" << "\n";
		} else {
			cout << "no" << "\n";
		}
	}
	return 0;
}