Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#define MAX 100
using namespace std;

int main() {
	int i, n, m, q, a, b, gnum = 0, ptog[MAX] = {};

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		if (ptog[a] != 0)ptog[b] = ptog[a];
		else if (ptog[b] != 0)ptog[a] = ptog[b];
		else ptog[a] = ptog[b] = ++gnum;
	}

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> a >> b;
		if (ptog[a] == ptog[b])cout << "yes\n";
		else cout << "no\n";
	}
}

C++: #include <bits/stdc++.h>

#define int             long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )    for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )  FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP              make_pair

using namespace std;
using P = pair<int, int>;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

vector<int> G[100001];
int color[100001];

void dfs( int v, int c ){
    color[v] = c;
    for( auto nv : G[v] ){
        if( color[nv] ) continue;
        dfs( nv, c );
    }
}

signed main(){
    int N, M; cin >> N >> M;
    REP( i, M ){
        int s, t; cin >> s >> t;
        G[s].push_back( t );
        G[t].push_back( s );
    }

    int c = 1;
    REP( i, N ){
        if( color[i] == 0 ){
            dfs( i, c );
            c++;
        }
    }

    int Q; cin >> Q;
    REP( i, Q ){
        int s, t; cin >> s >> t;
        if( color[s] == color[t] ){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_map>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< vector<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< vector<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).push_back(t);
		friendGraphList.at(t).push_back(s);
	}
	for (auto& v : friendGraphList)v.shrink_to_fit();
}

const vector<bool>& accesibleNodeSearch(const vector<vector<int>>& graphList
	, unordered_map<int, vector<bool>>& visitMatrix
	, const int s
	) 
{
	pair<unordered_map<int, vector<bool>>::iterator, bool> result = visitMatrix.emplace(s,vector<bool>());
	vector<bool>& visitList = result.first->second;
	if (result.second) {//未計算の場合のみ計算する
		const size_t n = graphList.size();
		queue<int> nodeQueue;
		visitList.resize(n,false);//resizeは必ずしもその値で埋めてくれない？
		nodeQueue.push(s);
		visitList.at(s).flip();
		while (!nodeQueue.empty()) {
			const int id = nodeQueue.front();
			const vector<int>& adjList = graphList.at(id);
			for (const auto& adjID : adjList) {
				if (visitList.at(adjID))continue;
				const vector<int>& idList = graphList.at(adjID);
				if (find(idList.begin(), idList.end(), id) == idList.end())continue;
				nodeQueue.push(adjID);
				visitList.at(adjID).flip();
			}
			nodeQueue.pop();
		}
	}
	return visitList;
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	unordered_map<int, vector<bool>> visitMatrix;
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		const vector<bool>& visitList = accesibleNodeSearch(graphList, visitMatrix, s);
		cout << (visitList.at(t) ? "yes" : "no") << endl;
	}
}

int main()
{


	execute(cin);


	return 0;
}