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 <numeric>
#include <algorithm>
#include <unordered_set>

using namespace std;

const int N_MAX = 100000;
vector<vector<int32_t>> A(N_MAX);
vector<bool> visited(N_MAX);
vector<int32_t> deadend;

void dfs1(int32_t u) {
    visited[u] = true;

    for (int32_t v : A[u]) {
        if (!visited[v]) {
            dfs1(v);
        }
    }

    deadend.push_back(u);

    return;
}

unordered_set<int32_t> dfs2(int32_t u, unordered_set<int32_t> &scc) {
    visited[u] = true;

    for (int32_t v : A[u]) {
        if (!visited[v]) {
            dfs2(v, scc);
        }
    }

    scc.insert(u);

    return scc;
}

int main() {
    fill(visited.begin(), visited.end(), false);

    int32_t n, m;
    cin >> n >> m;

    for (int32_t i = 0; i < m; i++) {
        int32_t s, t;
        cin >> s >> t;

        A[s].push_back(t);
        A[t].push_back(s);
    }

    for (int32_t i = 0; i < n; i++) {
        if (!visited[i]) {
            dfs1(i);
        }
    }

    reverse(deadend.begin(), deadend.end());
    fill(visited.begin(), visited.end(), false);
    vector<unordered_set<int32_t>> sccs;

    for (int32_t u : deadend) {
        if (!visited[u]) {
            unordered_set<int32_t> scc;
            dfs2(u, scc);
            sccs.push_back(scc);
        }
    }

    int32_t q;
    cin >> q;

    for (int32_t i = 0; i < q; i++) {
        int32_t s, t;
        cin >> s >> t;

        bool flag = false;
        for (auto scc : sccs) {
            if (scc.find(s) != scc.end() && scc.find(t) != scc.end()) {
                flag = true;
                break;
            }
        }

        cout << (flag ? "yes" : "no") << endl;
    }

    return 0;
}