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 pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

class Union_Find{
  public:
  Union_Find(){};
  static const ll MAX_N = 100000*2+1;
  int par[MAX_N];
  int ranks[MAX_N];
  void init(int n){
    rep(i,MAX_N){
      par[i] = i;
      ranks[i] = 0;
    }
  }
  int find(int x){
    if(par[x] == x){
      return x;
    }
    else{
      return par[x] = find(par[x]);
    }
  }
  void Unite(int a, int b){
    a = find(a);
    b = find(b);
    if(a == b) return ;
    if(ranks[a] < ranks[b]){
      par[a] = b;
    }
    else{
      par[b] = a;
      if(ranks[a] == ranks[b]) ranks[a]++;
    }
  }
  bool same(int a,int b){
    return find(a) == find(b);
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  Union_Find uf;
  ll n,m;
  cin >> n >> m;
  uf.init(n);
  ll s,t;
  rep(i,m){
    cin >> s >> t;
    uf.Unite(s,t);
  }
  ll q;
  cin >> q;
  rep(i,q){
    cin >> s >> t;
    if(uf.same(s,t)){
      cout << "yes" << endl;
    }
    else{
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
vector< vector<int> > M;
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, M[s].size()) {
      int t = M[s][i];
      if (!V[t]) {
        q.push(t);
        V[t] = true;
      }
    }
  }

  return V[v];
}

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

  cin >> n >> m;
  REP(i, n) {
    vector<int> vc;
    M.push_back(vc);
  }
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u].push_back(v);
    M[v].push_back(u);
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <stack>
using namespace std;

int N,M;
const int maxn = 100000+5;
const int WHITE = 0;
vector<int> G[maxn];
vector<int> nodeColor;

void DFS(int u,int color){
    stack<int> nodeStack;
    nodeStack.push(u);
    while(!nodeStack.empty()){
        int u = nodeStack.top();
        nodeStack.pop();
        nodeColor[u] = color;
        for(int i = 0;i<G[u].size();i++){
            int v = G[u][i];
            if(nodeColor[v]==0) nodeStack.push(v);
        }
    }
}

int main(){
    //freopen("data2.txt","r",stdin);
    cin >> N >> M;
    nodeColor.resize(N,0);
    int u,v;
    for(int i = 0;i<M;i++){
        scanf("%d %d",&u,&v);
        G[u].push_back(v);
        G[v].push_back(u);
    }
    int color = 1;
    for(int i = 0;i<N;i++) if(nodeColor[i]==0) DFS(i,color++);
    int q;
    scanf("%d",&q);
    for(int i = 0;i<q;i++){
        scanf("%d %d",&u,&v);
        if(nodeColor[u]==nodeColor[v]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

C++: #include<stdio.h>
#include<queue>
#include<algorithm>

#define MAX 100000

int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;


bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;

    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }

    return false;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < m; ++i) {
        int u,k;
        scanf("%d %d", &u, &k);
        G[u][k] = 1;
        G[k][u] = 1;
    }

    int q;
    scanf("%d", &q);
    for (int i = 1; i <= q; ++i) {
        int s,d;
        scanf("%d %d", &s, &d);
        bfs(s, d) ? printf("yes\n") : printf("no\n");
    }

}