Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	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())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	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 root(s, &group) == root(t, &group) {
			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<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

void dfs(int s, int group, vector<vector<int>> v, vector<int> &used) {
	used[s] = group;
	int n = v.size();
	int k = v[s].size();
	REP(l, k) {
		int j = v[s][l];
		if (!used[j]) {
			dfs(j, group, v, used);
		}
	}
}

void divide(vector<vector<int>> v, vector<int> &used) {
	int s = 0;
	int group = 1;
	int n = v.size();
	while (s < n) {
		dfs(s, group++, v, used);
		while (s < n && used[s]) s++;
	}
}

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

	int n, m;
	cin >> n >> m;
	vector<vector<int>> v(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	vector<int> used(n, 0);
	divide(v, used);
	int q;
	cin >> q;
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		if (used[s] == used[t]) cout << "yes\n";
		else cout << "no\n";
	}
	

	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define max 100000
vector<int> vn[max];
int grp[max];
int n;

void grouping(int g,int cnt){
  if(grp[g]!=0)return;
  grp[g]=cnt;
  for(int i=0;i<(int)vn[g].size();i++)
    grouping(vn[g][i],cnt);
}

int main(){
  int m,t,u,cnt;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>t>>u;
    vn[t].push_back(u);
    vn[u].push_back(t);
  }
  cnt=1;
  for(int i=0;i<n;i++){
    if(grp[i]!=0)continue;
    grouping(i,cnt);
    cnt++;
  }
  //for(int i=0;i<n;i++)cout<<"i: "<<i<<"grp "<<grp[i]<<endl;
  cin>>m;
  for(int i=0;i<m;i++){
    cin>>t>>u;
    if(grp[t]==grp[u]&&grp[t]!=0)cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#define N_MAX 100000
using namespace std;

vector<int> sns[N_MAX];
int* group;
int g = 0;

void dfs(int s) {
  if (group[s] != 0) return;
  group[s] = g;
  
  int len = sns[s].size();
  for (int i = 0; i < len; i++) {
    dfs(sns[s][i]);
  }
}

int main() {
  int n,m,q,s,t;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    cin >> s >> t;

    sns[s].push_back(t);
    sns[t].push_back(s);
  }

  // dfs
  group = new int[n];
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      g++;
      dfs(i);
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (group[s] == group[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (1E9+7)
#define PI 3.1415926535897932384

std::ostream &operator<<(std::ostream &out, const vector<int> &tgt)
{
	string s;
	for (int i = 0; i<tgt.size(); i++) {
		s += (to_string(tgt[i]) + ((i != tgt.size() - 1) ? " " : ""));
	}
	out << s;
	return out;
}

vi rnk,par;

int find(int x){
	if (par[x] == x) return x;
	return par[x] = find(par[x]);
}

void unite(int x, int y) {
	x = find(x);
	y = find(y);
	if (x == y)return;
	if (rnk[x] < rnk[y]) {
		par[x] = y;
	}
	else {
		par[y] = x;
		if (rnk[x] == rnk[y])rnk[x]++;
	}
}

bool same(int x, int y) {
	return (find(x) == find(y));
}

int main() {
	int n,m,a,b,q;
	cin >> n >> m;
	par.resize(n);
	rnk.resize(n);
	rep(i, n) {
		par[i] = i;
		rnk[i] = 0;
	}

	rep(i, m) {
		cin >> a >> b;
		unite(a, b);
	}
	cin >> q;
	rep(i, q) {
		cin >> a >> b;
		cout << (same(a, b) ? "yes" : "no") << endl;
	}

}

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

const int MAX = 100000;
const int NIL = -1;

int n;
std::vector<int> G[MAX];
int state[MAX];

void dfs(int u, int id) {
  	
	stack<int> S;
	S.push(u);
	state[u] = id;
	
	while( !S.empty() ) {
		int t = S.top();S.pop();
		for( int i = 0; i < G[t].size(); i++ ) {
			int v = G[t][i];
			if(state[v] == NIL ) {
				state[v] = id;
			    S.push(v);
		    }
		}
		
	}	

}

void assignState(){
	int id = 1;
	for(int i = 0; i < n; i++ ) state[i] = -1;
	for(int u = 0; u < n; u++ ) {
		if( state[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);
    }
    
    assignState();

    cin >> q;

    for( int i = 0; i < q; i++ ){
    	cin >> s >> t;
    	if( state[s] == state[t] ) cout << "yes" << endl;
    	else cout << "no" << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>

using namespace std;

#define N_MAX 100000

int search_connection(vector<int> v, int t, stack<int> st);
vector<int> G[N_MAX];

int main()
{
    int n, m, i, s, t, q;
    stack<int> st;

    cin >> n >> m;

    //??£??\??????????????????
    for (i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
    }

    //??????????????????
    cin >> q;
    for (i = 0; i < q; i++) {
        cin >> s >> t;
        while (st.size() != 0) st.pop();

        st.push(s);
        if ( search_connection(G[s], t, st) )
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }

    return 0;
}

int search_connection(vector<int> v, int t, stack<int> st) {
    for (auto itr = v.begin(); itr != v.end(); itr++) {
        if (*itr == t)
            return 1;
        else {
            st.push(*itr);
            return (0 || search_connection(G[*itr], t, st));
        }
    }
}