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 <stdio.h>
#include <algorithm>
#include <queue>

using namespace std;

struct Info{
	Info(){s = t = -1;}
	Info(int tmp_s,int tmp_t){
		s = tmp_s;
		t = tmp_t;
	}
	int s;
	int t;
};

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		global = new Info[m];
		each = new int*[n];
		each_index = new int[n];
		num_of_each = new int[n];
		group = new int[n];
		for(int i=0;i<n;i++){
			each_index[i] = 0;
			num_of_each[i] = 0;
			group[i] = 0;
		}
		global_index = 0;
		group_number = 1;
	}

	void push(int s,int t){
		global[global_index++] = Info(s,t);
		num_of_each[s]++;
		num_of_each[t]++;
	}

	void allocate(){
		for(int i=0;i<n;i++){
			if(num_of_each[i] > 0){
				each[i] = new int[num_of_each[i]];
			}
		}
		Info tmp;
		for(int i=0;i<m;i++){
			tmp = global[i];
			each[tmp.s][each_index[tmp.s]++] = tmp.t;
			each[tmp.t][each_index[tmp.t]++] = tmp.s;
		}
	}

	void makeGroup(){
		int node,i;
		queue<int> Q;
		Q.push(0);
		group[0] = 1;
		while(true){
			while(!Q.empty()){
				node = Q.front();
				Q.pop();
				if(num_of_each[node] > 0){
					for(int k=0; k < num_of_each[node];k++){
						if(group[each[node][k]] == 0){
							group[each[node][k]] = group[node];
							Q.push(each[node][k]);
						}
					}
				}
			}
			group_number++;

			for(i=0;i<n;i++){
				if(group[i] == 0){
					group[i] = group_number;
					Q.push(i);
					break;
				}
			}
			if(i == n) break;
		}
	}

	bool canAccsess(int s,int t){
		return group[s] == group[t];
	}

private:
	Info* global;
	int** each;
	int* each_index;
	int* num_of_each;
	int* group;
	int group_number;
	int global_index,n,m;
};

int main(){
	int n,m,s,t,q;
	scanf("%d %d",&n,&m);

	Relation relation(n,m);
	for(int i=0;i<m;i++){
		scanf("%d %d",&s,&t);
		relation.push(s,t);
	}
	relation.allocate();
	relation.makeGroup();
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d %d",&s,&t);
		if(relation.canAccsess(s,t)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
    return 0;
}

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

bool DFS(vector<vector<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, int> > S;
    vector<bool> is_visited(G.size(), false);

    int idx = 0;
    do {
        if (idx >= G[a].size()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
            idx++;
            continue;
        }
        if (G[a][idx] == b) return true;
        if (!is_visited[G[a][idx]]) {
            is_visited[G[a][idx]] = true;
            S.push(make_pair(a, idx));
            a   = G[a][idx];
            idx = 0;
            continue;
        }
        idx++;
    } while (!S.empty());
    return false;
}


int main(){
    int n, m;
    cin >> n >> m;

    vector<vector<int> > G(n);
// bool D[100000][100000];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>

void numbering(int id, int *g, int num, std::vector<int> *a){
    int tmp;
    for(int i=0;i<a[id].size();i++){
        tmp = a[id][i];
        if(g[tmp])continue;
        g[tmp] = num;
        numbering(tmp, g, num, a);
    }
}

void grouping(std::vector<int> *a, int *g, int n){
    int num = 1;
    for(int i=0;i<n;i++){
        if(g[i])continue;
        g[i] = num;
        numbering(i,g,num,a);
        num++;
    }
}

int main(){
    int i, n, m, q, s, t;
    int *g;
    scanf("%d %d",&n,&m);
    g = (int *)malloc(sizeof(int)*n);
    memset(g, 0, sizeof(int)*n);
    std::vector<int> a[n];
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        a[s].push_back(t);
        a[t].push_back(s);
    }
    scanf("%d",&q);
    grouping(a,g,n);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(g[s]==g[t])printf("yes\n");
        else printf("no\n");
    }
    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 (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}

vi par;
vi ran;

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

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

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


int main() {
	int n, m,a,b;
	cin >> n >> m;
	par.resize(n);
	rep(i, n) {
		par[i] = i;
	}
	ran.resize(n,0);
	rep(i, m) {
		cin >> a >> b;
		unite(a, b);
	}
	cin >> m;
	rep(i, m) {
		cin >> a >> b;
		cout << (same(a, b) ? "yes" : "no") << endl;
	}
}