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

C++: #include <iostream>
#include <vector>
#include <stack>
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(c);
      }
    }
  }
}

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(void){
  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 <iostream>
#include <algorithm>
#define USER_SIZE 100000
#define NIL -1

using namespace std;

int group[USER_SIZE];

int update(int num){
    if(num == group[num]){ return num; }
    else{ return group[num] = update(group[num]); }
}

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

    cin >> n >> m;
    for(int i = 0; i < n; i++){
        group[i] = i;
    }
    for(int i = 0; i < m; i++){
        cin >> s >> t;
        int tmp1 = update(s);
        int tmp2 = update(t);
        group[max(tmp1, tmp2)] = min(tmp1, tmp2);
    }

    cin >> q;
    while(q--){
        cin >> s >> t;
        if(update(s) == update(t)){ cout << "yes" << endl; }
        else{ cout << "no" << endl; }
    }

    return 0;
}

C++: #include <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s][t] = 1;
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}