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