Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include <bits/stdc++.h>

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int N_MAX = 100'000;

Vi adj[N_MAX];
int color[N_MAX];

int n;
void build_color() {
    bool isqueued[N_MAX] = {false};
    for (int i = 0, c = 0; i < n; ++i) {
        if (isqueued[i]) { continue; }
        queue<int> q;
        q.push(i);
        isqueued[i] = true;
        while (!q.empty()) {
            const int j = q.front();
            color[j] = c;
            for (const int k : adj[j]) {
                if (isqueued[k]) { continue; }
                q.push(k);
                isqueued[k] = true;
            }
            q.pop();
        }
        ++c;
    }
}

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

    int m;
    cin >> n >> m;

    int a, b;
    FOR(i,0,m) {
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    /*
    cout << "adj read." << endl;
    FOR(i,0,n) {
        for (const int j : adj[i]) {
            cout << " " << j;
        }
        cout << endl;
    }
    */
    build_color();

    int q;
    cin >> q;
    FOR(i,0,q) {
        cin >> a >> b;
        cout << (color[a] == color[b] ? "yes" : "no") << endl;
    }

    return 0;
}

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

using namespace std;

const int WHITE = 0, GREY = 1, BLACK = 2;

struct CC {
    int n;
    vector<vector<int>> g;
    vector<int> color;
    vector<int> component;
    int cId = 0; //component id
    
    CC (int n) : n(n) {
        g.assign(n, vector<int>(0));
        color.assign(n, WHITE);
        component.assign(n, -1);
    }
    
    void addEdge (int u, int v) {
        g[u].push_back(v);
        g[v].push_back(u);
    }
    
    void solve () {
        for (int i = 0; i < n; i++) {
            if (color[i] == WHITE) {
                cId++;
                dfs(i);
            }
        }
    }
    
    void dfs (int u) {
        color[u] = GREY;
        component[u] = cId;
        for (int v : g[u]) {
            if (color[v] == WHITE) {
                dfs(v);
            }
        }
        color[u] = BLACK;
    }
};

int main() {
    
    int n, m;
    cin >> n >> m;
    CC cc(n);
    
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        cc.addEdge(u, v);
    }
    
    cc.solve();
    
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        if (cc.component[u] == cc.component[v]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
    
    return 0;
}

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

vector<vector<int> > G;
vector<int> is_visited;
int b;

bool DFS(int a, int i) {
    if (a == b) return true;
    if (i >= G[a].size()) return false;
    is_visited[a] = 1;
    if (!is_visited[G[a][i]] && DFS(G[a][i], 0)) {
        return true;
    }
    return DFS(a, i + 1);
}

// bool DFS(const vector<vector<int> >& G,
//          vector<bool>& is_visited, int a, int b, int i) {
//     if (a == b) return true;
//     if (i >= G[a].size()) return false;
//     is_visited[a] = true;
//     if (!is_visited[G[a][i]] && DFS(G, is_visited, G[a][i], b, 0)) {
//         return true;
//     }
//     return DFS(G, is_visited, a, b, i + 1);
// }

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

    G.resize(n);
    is_visited.resize(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;
        cin >> a >> b;
        for (int j = 0; j < n; j++) {
            is_visited[j] = 0;
        }
        if (DFS(a, 0)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX 101000

int ran[MAX], par[MAX];

int init(int n){
	for(int i = 0; i < n; i++){
		ran[i] = 0;
		par[i] = i;
	}
}

int find(int x){
	return par[x] ==  x ? x : par[x] = find(par[x]);
}

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

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


int main(){
	int n, m, q, s, t;
	
	cin>>n>>m;
	
	init(n);
	
	for(int i = 0; i < m; i++){
		cin>>s>>t;
		unite(s,t);
	}
	
	cin>>q;
	
	for(int i = 0; i < q; i++){
		cin>>s>>t;
		cout<<(same(s,t)?"yes":"no")<<endl;
	}
	
	
	return 0;
}