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>
using namespace std;
#include<stdio.h>

typedef struct box { int val; box *prev, *next; } box_t;

box_t* init()
{
    box_t* head = new box_t;
    head->val = -1;
    head->next = NULL;
    head->prev = NULL;
    return head;
}

box_t* make_tail(box_t* head)
{
    box_t* tail = new box_t;
    tail->val = -1;
    head->next = tail;
    tail->next = NULL;
    tail->prev = head;
    return tail;
}

bool isEmpty(box_t* head, box_t* tail)
{
    return (head->next == tail ? true : false);
}

box_t* f_insert(box_t* head, int data)
{
    box_t* box = new box_t;
    box->val = data;
    box->next = head->next;
    box->prev = head;
    head->next->prev = box;
    head->next = box;
    return box;
}

int b_pop(box_t* tail)
{
    if(!(tail->prev->prev)) return -1;
    box_t* box = tail->prev;
    int val;
    val = tail->prev->val;
    tail->prev = box->prev;
    box->prev->next = tail;
    delete box;
    return val;
}

void clear(box_t* head, box_t* tail)
{
    box_t* box1 = head->next;
    box_t* box2;
    if(box1 == tail) return;
    while(box1 != tail){
        box2 = box1->next;
        delete box1;
        box1 = box2;
    };
    head->next = tail;
    tail->prev = head;
    return;
}

int main()
{
    int G[3000][3000];
    int dist[3000];

    box_t* head = init();
    box_t* tail = make_tail(head);

    int n, i, j, e, q, v1, v2;
    scanf("%d", &n);
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++){ G[i][j] = 0; } }
    int m;
    scanf("%d", &m);
    for(int k = 0; k < m; k++){
        scanf("%d %d", &i, &j);
        G[i][0]++; G[j][0]++;
        G[i][G[i][0]] = j;
        G[j][G[j][0]] = i;
    }
    scanf("%d", &q);
    int self, friends;
    bool is_connected;

    for(int k = 0; k < q; k++){
        for(j = 0; j < n; j++) dist[j] = -1;

        scanf("%d %d", &self, &friends);
        f_insert(head, self);
        dist[self] = 0;
        is_connected = false;

        while(1){
            v1 = b_pop(tail);
            e = G[v1][0];
            for(i = 1; i <= e; i++){
                v2 = G[v1][i];
                if(dist[v2] < 0){
                    dist[v2] = dist[v1] + 1;
                    f_insert(head, v2);
                }
            }
            if(dist[friends] > 0){
                is_connected = true;
                break;
            }
            if(isEmpty(head, tail)) break;
        };
        if(is_connected){ printf("yes\n"); }else{ printf("no\n"); }
        if(!isEmpty(head, tail)) clear(head, tail);
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

struct UnionFind{
    vector<int> par;
    UnionFind(int N) : par(N){
        for(int i = 0; i < N; i++){
            par[i] = -1;
        }
    }
    int root(int x){
        if(par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    void unite(int x, int y){
        int rx = root(x);
        int ry = root(y);
        if(rx == ry) return;
        else{
            if(par[rx] > par[ry]) swap(rx, ry);
            par[rx] += par[ry];
            par[ry] = rx;
       }
    }
    bool same(int x, int y){
        return root(x) == root(y);
    }
    void check(){
        for(auto x : par){
            cout << x << " ";
        }
        cout << endl;
    }
};

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

    int n, m;
    cin >> n >> m;
    
    UnionFind uf(n);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        uf.unite(a, b);
    }

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

    return 0;
}

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

int fri[100010];

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

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

void uf_union(int x, int y){
  x = find(x);
  y = find(y);
  fri[x] = y;
}

int main(){

  int n,m,q;
  cin >> n >> m;

  // init
  for(int i=0; i<n; i++) fri[i] = i;

  //
  for(int i=0; i<m; i++){
    int u,v; scanf("%d %d",&u,&v);
    uf_union(u,v);
  }

  cin >> q;
  for(int i=0; i<q; i++){
    int u,v; scanf("%d %d",&u,&v);
    if( find(u) == find(v) ) printf("yes\n");
    else printf("no\n");
  }


}

C++: #include <stdio.h>
#include <string.h>
#define N 100010
int father[N];
int findfather(int x){
    if(x == father[x]) return x;
    else return findfather(father[x]);
}
int main(){
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 0; i < n; i++) {
        father[i] = i;
    }
    for(int k = 0; k < m; k++){
        int a, b;
        scanf("%d %d", &a, &b);
        father[findfather(b)] = findfather(a);
    }

//    for(int i = 0; i < n; i++){
//        printf("i = %d father = %d\n", i, father[i]);
//    }
    int q;
    scanf("%d", &q);
    for(int k = 1; k <= q; k++){
        int x, y;
        scanf("%d %d", &x, &y);
        if(findfather(x) == findfather(y)) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

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

using namespace std;

const int MAX = 100000;

vector<int> G[MAX];
int C[MAX] = {0};

void dfs(int u, int c) {
    if (C[u] == 0) {
        C[u] = c;
        for (auto v : G[u]) {
            if (C[v] == 0) dfs(v, c);
        }
    }
}

int main() {

    int n, m;
    cin >> n >> m;

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

    int c = 1;
    for (int i = 0; i < n; i++) {
        if (C[i] == 0) dfs(i, c++);
    }

    //
    int q;
    cin >> q;

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