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++: ////////////////////////////////////////
//??°??????????????????
//ALDS1_11_A:   Graph
//ALDS1_11_B:   Depth First Search
//ALDS1_11_C:   Breadth First Search
//????????§????????\?????????????£?
//
//ALDS1_11_D:   Connected Components
//
////////////////////////////////////////

#include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream

//#define NDEBUG //NDEBUG???#include <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

//?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif


#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << setw(3) << (A) << " " // << setw(3) ??????????????\????????????
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;


const int INF = 1e9-1;

int NN;
const int N_MAX = 100000;

bool edge[N_MAX][N_MAX] = {0};
int color[N_MAX] = {0}; //??£?????°???????????¨???1??\????????°?????§???????????????
int currentColor = 0;



void dfs(int i) { //depth first search
    
    if(color[i]!=0) return; //?????§????¨??????????
    
    //??????????????????????¨??????§????????°?????????????????????????????????????????????????????????dfs????????°??????????????????
    color[i] = currentColor;
    rep(j,0,NN) {
        if(edge[i][j]) dfs(j);
    }
    
    //???????????????????????????dfs??????????????????????????????????????£??????
    return;
    
}



int main() {
    int M;
    din>>NN>>M;
    
    int v1, v2;
    rep(i,0,M) {
        din>>v1>>v2;
        edge[v1][v2] = edge[v2][v1] = 1;
    }
    
    rep(i,0,NN) edge[i][i] = 1;
    
    
    
//    //test display
//    dout<<endl;
//    dout<<"       dist = ";
//    rep(i,0,NN) disP(i);
//    dout<<endl;
//    rep(i,0,NN) {
//        disP(i); dispAll(edge[i], NN);
//    }
//    dout<<endl;
    
//    //test display
//    dout<<"    i = ";
//    rep(i,0,NN) disP(i);
//    dout << endl;
//    dispAll(color, NN);
    
    
    //execute DFS (Depth First Search)
    //??±???????????¢?´¢??§??¨??????????????¢?´¢?????????????????°?????????????????????????£??????????????????????????????????
    rep(i,0,NN) {
        if(color[i]==0) { //?????????????????????????????????dfs
            currentColor++;
            dfs(i);
        }
    }
    
//    //test display
//    dout<<"    i = ";
//    rep(i,0,N) disP(i);
//    dout << endl;
//    dispAll(color, N);
    
    //output
    int Q; din>>Q;
    rep(i,0,Q) {
        din >> v1 >> v2;
        
        dout << (color[v1]==color[v2] ? "yes" : "no") << endl;
    }
    
    
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

void dfs(int id, int gid, vector<vector<int> >& g, int group[]){
	group[id] = gid;
	for(int n : g[id]){
		if(group[n] < 0){
			group[n] = gid;
			dfs(n, gid, g, group);
		}
	}
	return;
}

int main(){
	int n, m; cin >> n >> m;
	int group[n];
	memset(group, -1, n*sizeof(int));
	vector<vector<int> > g(n, vector<int>());
	rep(i, m){
		int s, t; cin >> s >> t;
		g[s].push_back(t);
		g[t].push_back(s);
	}

	int gid = 0;
	rep(i, n) if(group[i] < 0) dfs(i, gid++, g, group);

	int q; cin >> q;
	rep(i, q){
		int s, t; cin >> s >> t;
		cout << ((group[s] == group[t]) ? "yes" : "no") << endl;
	}

	return 0;
}