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

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

bool DFS(const 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<bool> > D(100000, vector<bool>(100000));

    vector<vector<int> > G(n);

    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 (int j = 0; j < G[i].size(); j++){
//         cout << G[i][j] << " ";
//     }
//     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 <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Node {
    int color = 0;
    vector<int> next;
} node[100000];

int main() {
    int n, m, q, t1, t2;

    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> t1 >> t2;
        node[t1].next.push_back(t2);
        node[t2].next.push_back(t1);
    }

    int color = 1;
    queue<int> que;
    while (1) {
        int start = -1;
        for (int i = 0; i < n; i++) {
            if (node[i].color == 0) {
                start = i;
                break;
            }
        }
        if (start == -1) {
            break;
        }
        
        node[start].color = color;
        que.push(start);
        while (!que.empty()) {
            int temp = que.front();
            int size = node[temp].next.size();
            for (int i = 0; i < size; i++) {
                if (node[node[temp].next[i]].color == 0) {
                    node[node[temp].next[i]].color = color;
                    que.push(node[temp].next[i]);
                }
            }
            que.pop();
        }
        color++;
    }

    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> t1 >> t2;
        if (node[t1].color == node[t2].color && node[t1].color > 0 && node[t2].color > 0) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }

    return 0;
}