Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

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