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<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
using namespace std;
vector<int> neighbors[100005];
//why choose vector, not matrix
int root[100005];

void dfs(int i, int start){
	root[i] = start;
	//visut all i's neighbors
	//c++ 11
	for(int j : neighbors[i]){
		if(root[j]==-1){
			dfs(j,start);
		}
	}
}

int main(){
	int n,m,x,y;
	memset(root, -1, sizeof(root));
	scanf("%d %d",&n,&m);
	for(int i = 0; i<m; i++){
		scanf("%d %d",&x,&y)
		neighbors[x].push_back(y);
		neighbors[y].push_back(x);
	}
	for(int i = 0; i<n; i++){
		if(root[x]= -1){
			dfs(i,i);
		}
	}
	scanf("%d", &q);
	for(int i = 0; i<q; i++){
		scanf("%d %d",&x, &y);
		if(root[x] = root[y]){
			printf("Yes\n");
		}else printf("no\n");
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define NIL -1
#define MAX 100000

ll conv(string num) { stringstream ss; ss << num << flush; ll n; ss>>n; return n; }

string conv(ll n) { stringstream ss; ss << n << flush; return ss.str(); }

vector<string> split(string s, string delim) {
    vector<string> res;
    int pos = 0;
    while(true) {
        int found = s.find(delim, pos);
        if(found >= 0) {
            res.push_back(s.substr(pos, found - pos));
        }
        else {
            res.push_back(s.substr(pos));
            break;
        }
        pos = found + delim.size();
    }
    return res;
}
int ALPH_SIZE = 26;
char ALPH_L[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char ALPH_U[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

int m, n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
    stack<int> s;
    s.push(r);
    color[r] = c; // set color to the init point 

    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;  // set same color to connected points
                s.push(v);
            }

        }
    }
}


void assignColor() {
    int id = 1;

    for(int i=0; i<MAX; i++) {
        color[i] = NIL;
    }

    for(int i=0; i<MAX; i++) {
        if (color[i] == NIL) {
            dfs(i, id++);
        }
    }
}



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

    assignColor();

    int q; 

    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++: 


/**
 *      purpose : 
 *      author  : kyomukyomupurin
 *      created : 
**/

// input/output
#include <iostream>
#include <fstream>
#include <sstream>
// container class
#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// math, algorithm
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
// etc
#include <bitset>
#include <cassert>
#include <cctype>
#include <cstring>
#include <chrono>
#include <iomanip>
#include <random>
#include <utility>
// using-directive
using namespace std;
// alias template
using int64 = long long;
using vi = vector<int>;
using vl = vector<int64>;
using pii = pair<int, int>;
using pll = pair<int64, int64>;
// text macro replacement
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define print(x) cout << (x) << '\n'
#define debug(x) cerr << #x << ": " << (x) << '\n'
#define dbg(v) for (size_t _ = 0; _ < v.size(); ++_){cerr << #v << "[" << _ << "] : " << v[_] << '\n';}
// variadic template
template<typename T> inline void chmin(T &a, T b) {if (a > b) a = b; return;}
template<typename T> inline void chmax(T &a, T b) {if (a < b) a = b; return;}
// constant
const int INF = (1<<30) - 1;
const int64 INF64 = (1LL<<62) - 1;
const int MOD = 1000000007;



struct UnionFind {
  std::vector<int> par, rank;

  UnionFind(int n) : par(n , -1), rank(n, 0) {}

  void init(int n) {
    par.assign(n, -1); rank.assign(n, 0);
  }

  int root(int x) {
    if (par[x] == -1) return x;
    else return par[x] = root(par[x]);
  }

  bool issame(int x, int y) {return root(x) == root(y);}

  void unite(int x, int y) {
    x = root(x); y = root(y);
    if (x == y) return;
    if (rank[x] < rank[y]) std::swap(x, y);
    if (rank[x] == rank[y]) ++rank[x];
    par[y] = x;
    return;
  }
};


int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m; cin >> n >> m;
  UnionFind uf(n);

  for (int i = 0; i < m; ++i) {
    int x, y; cin >> x >> y;
    if (!uf.issame(x, y)){
      uf.unite(x, y);
    }
  }

  int query; cin >> query;

  for (int i = 0; i < query; ++i) {
    int z, w; cin >> z >> w;
    if (uf.issame(z, w)) {
      cout << "yes" << '\n';
    } else {
      cout << "no" << '\n';
    }
  }

  return 0;
}