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>
#include <string>
#include <algorithm>
#include <utility>
#include <numeric>
#include <array>
#include <cmath>
#include <memory>
using namespace std;
//変数デバッグ
#define DEB(variable) cout << #variable << '=' << variable << endl

//for簡易表記(引数ミス防止)
#define FOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin); (LoopVariable) < (numberOFend); (LoopVariable)++)
#define DEFOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin)-1; (LoopVariable) >= (numberOFend); (LoopVariable)--)
#define REP(LoopVariable,numberOFend) for(int LoopVariable = 0;(LoopVariable)<(numberOFend);LoopVariable++)

constexpr long long int linf = numeric_limits<long long int>::max()-5;
constexpr int inf= numeric_limits<int>::max()-5;
constexpr long long int mod = 1000000007;


//0~n-1というデータを扱うことが前提となっている
//unique_ptr<UFtree<int>> uf(new UFtree<int>(n));
template <typename T>
class UFtree{
public:
    UFtree(int data_num){
        v.resize(data_num);
        for (int i = 0; i < data_num; i++){
            v[i] = i;
        }
    }
    ~UFtree(){}

    T get_root(T a){
        T result;
        if(v[a]==v[v[a]]){
            return v[a];
        }else{
            T result;
            vector<T> path;
            T now_where=a;
            while(v[now_where]!=v[v[now_where]]){
                path.push_back(now_where);
                now_where=v[now_where];

            }
            for(T &x:path){
                v[x] = v[now_where];
            }
            return v[now_where];
        }
    }

    //aにbをつける
    void attach(T a,T b){
        v[get_root(b)] = v[get_root(a)];
    }

    
    
    void check(){for(T &x:v){
            cout << x<<" ";
        }
        cout << endl;
    }

  private:
    vector<T> v;


};



int main(){

    int n, m;
    cin >> n >> m;
    unique_ptr<UFtree<long long int>> uf(new UFtree<long long int>(n));

    long long int tmpa, tmpb;
    REP(i,m){
        scanf("%lld %lld", &tmpa, &tmpb);
        uf->attach(tmpa,tmpb);
    }

    int q;
    cin >> q;

    REP(i,q){
        scanf("%lld %lld", &tmpa, &tmpb);
        //cout << uf->get_root(tmpa) << " " << uf->get_root(tmpb) << endl;
        if(uf->get_root(tmpa)==uf->get_root(tmpb)){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }
    //uf->check();

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
#define MAX 100000
#define NIL -1
vector<int> G[MAX];
stack<int> S;
int color[MAX],n,m;
void dfs(int u,int id){
    S.push(u);
    color[u] = id;
    while(!S.empty()){
        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] = id;
                S.push(v);
            }
        }
    }
}
void dfs_init(){
    int id = 0;
    for(int i=0;i<n;i++){
        color[i] = NIL;
    }
    for(int i=0;i<n;i++){
        if(color[i]==NIL)
            dfs(i,id++);
    }

}
int main()
{
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs_init();
    int num;
    cin>>num;
    for(int i=0;i<num;i++){
        int u,v;
        cin>>u>>v;
        if(color[u]==color[v]){
            cout<<"yes"<<endl;
        }else{
            cout<<"no"<<endl;
        }
    }
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)

vector<int> adjacencyList[100005];//??£??\?????????

int n;
int color[100005];//????????????????????°?????????????±???????????????????


void assignColor() {
	REP(i, n) color[i] = -1; //?????????

	//?????????
	color[0] = 0;
	int next_index;
	REP(i, n) {
		if (color[i] == -1) color[i] = i;//????????????????????????i??§??????
		//???????????????????????????????????????????????\?????????????????????

		for (int j = 0;j < adjacencyList[i].size();j++) {
			next_index = adjacencyList[i][j]; //??£??\????????????????¬?????????????????????????
			if (color[next_index] == -1) {//??????????????????????????????i????????§??????
				color[next_index] = color[i];
			}
		}
	}
}

bool IsConCom(int s, int t) {
	//s,t???????????°?????????(???)?????????
	return color[s] == color[t];
}

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

	int s, t;
	REP(i, m) {
		cin >> s >> t;
		adjacencyList[s].push_back(t);
		adjacencyList[t].push_back(s);
	}

	assignColor();//?????????

	int q;
	cin >> q;
	REP(i, q) {
		cin >> s >> t;//s??????t?????????????????¨???????????¨???

		(IsConCom(s, t)) ? cout << "yes" : cout << "no";
		cout << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <stdio.h>
using namespace std;
 
int main() {
int d[100100];
int m,n,q;
scanf("%d %d", &n,&m);
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
d[j]=h;
while(a!=h ||b!=h){
 
    d[a]=h;
    d[b]=h;
a=d[a];
b=d[b];
}
}

scanf("%d",&q);
for(int y=0;y<q;y++){
    int a,b;
scanf("%d %d", &a,&b); 
        int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

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

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