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