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


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

C++: #include <stdio.h>
#include <iostream>
#include <queue>
using namespace std;

#define NODE_NUM 100000

queue<int> que;

class Node
{
private:
	int next_num;
	int next_node_list[NODE_NUM];
public:
	Node();
	void AddNextNode(int id);
	bool Search(int st, int ed);
	bool check[NODE_NUM];
	bool link[NODE_NUM];
};

Node::Node()
{
	next_num = 0;
	for (int i = 0; i < NODE_NUM; i++) {
		check[i] = false;
	}
	for (int i = 0; i < NODE_NUM; i++) {
		link[i] = false;
	}
}

void Node::AddNextNode(int id)
{
	next_node_list[next_num] = id;
	next_num++;
}

Node *Node_list;

bool Node::Search(int st, int ed)
{
	if (link[ed]) return true;

	for (int k = 0; k < next_num; k++) {
		Node_list[st].link[next_node_list[k]] = true;
	}

	for (int i = 0; i < next_num; i++) {
		if (next_node_list[i] == ed) return true;
	}
	
	for (int j = 0; j < next_num; j++) {
		if (Node_list[next_node_list[j]].check[ed] == 0) {
			Node_list[next_node_list[j]].check[ed] = 1;
			que.push(next_node_list[j]);
		}
	}
	
	return false;
}

int main(void)
{
	//input
	int n;
	cin >> n;
	Node_list = new Node[n]();
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		Node_list[s].AddNextNode(t);
		Node_list[t].AddNextNode(s);
	}
	
	//output
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		bool ret = Node_list[s].Search(s, t);
		while (!ret && !que.empty()) {
			ret = Node_list[que.front()].Search(s, t);
			que.pop();
		};
		while (!que.empty()) {
			que.pop();
		}

		if (ret) {
			cout << "yes" << "\n";
		}
		else {
			cout << "no" << "\n";
		}
	}
	
	return 0;
}

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

using namespace std;

class UnionFind
{
public:
	vector<int> par;
	vector<int> rk;
	UnionFind(int n)
	{
		par.resize(n);
		rk.resize(n);
		for (int i = 0; i < n; ++i)
		{
			par[i] = i;
		}
	}
	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 (rk[x] < rk[y])
		{
			par[x] = y;
		}
		else
		{
			par[y] = x;
			if (rk[x] == rk[y])
			{
				++rk[x];
			}
		}
	}
	bool same(int x, int y)
	{
		return(find(x) == find(y));
	}
};


void solve()
{
	int n, m;
	cin >> n >> m;
	UnionFind uf(n);
	for (int i = 0; i < m; ++i)
	{
		int s, t;
		cin >> s >> t;
		uf.unite(s, t);
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i)
	{
		int s, t;
		cin >> s >> t;
		if (uf.same(s, t))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <stdio.h>
#include <iostream>
using namespace std;

class Node{
private:
	int id;
	int friend_num;
	Node **friends;
	int check_id;
public:
	Node(int id, int n);
	~Node(){ delete[] friends; }
	void addFriend(Node *friend_node);
	bool searchFriend(int id);
};

Node::Node(int id, int n)
{
	this->id = id;
	friend_num = 0;
	friends = new Node*[n];
	check_id = -1;
}

void Node::addFriend(Node *friend_node)
{
	friends[friend_num] = friend_node;
	friend_num++;
}

bool Node::searchFriend(int id)
{
	if (check_id == id) return false;
	check_id = id;

	if (this->id == id) {
		check_id = -1;
		return true;
	}

	for (int i = 0; i < friend_num; i++) {
		if (friends[i]->searchFriend(id)) {
			check_id = -1;
			return true;
		}
	}
	check_id = -1;
	return false;
}

int main(void)
{
	int n;
	cin >> n;	
	Node **node = new Node*[n];
	for (int i = 0; i < n; i++) node[i] = new Node(i, n);

	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int s, t;
		cin >> s >> t;
		node[s]->addFriend(node[t]);
		node[t]->addFriend(node[s]);
	}
		
	int q;
	cin >> q;
	for (int i = 0; i < q; i++) {
		int s, t;
		cin >> s >> t;
		bool result = node[s]->searchFriend(t);
		if (result) cout << "yes" << endl;
		else cout << "no" << endl;
	}

	delete[] node;
	return 0;
}

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

bool x[10000][10000];
bool y[10000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][b]=true;
		x[b][a]=true;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<50;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<n;k++){
						if(x[j][k]==true){
							y[k]=true;
						}
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}