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 <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100'000;

Vi adj[N_MAX];

int n;
bool isconnected(const int a, const int b) {
    bool isqueued[N_MAX] = {false};
    queue<int> q;
    q.push(a);
    isqueued[a] = true;
    while (!q.empty()) {
        const int i = q.front();
        //cout << "#" << i << endl;
        if (i == b) { return true; }
        for (const int j : adj[i]) {
            if (isqueued[j]) { continue; }
            q.push(j);
            isqueued[j] = true;
        }
        q.pop();
    }
    return false;
}

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

    int m;
    cin >> n >> m;

    int a, b;
    FOR(i,0,m) {
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    /*
    cout << "adj read." << endl;
    FOR(i,0,n) {
        for (const int j : adj[i]) {
            cout << " " << j;
        }
        cout << endl;
    }
    */

    int q;
    cin >> q;
    FOR(i,0,q) {
        cin >> a >> b;
        cout << (isconnected(a, b) ? "yes" : "no") << endl;
    }

    return 0;
}

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