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>
#include <stack>
#include <vector>

using namespace std;


vector<int> vertexes[100000];
stack<int> visiting;
bool discovered[100000];
int belong[100000];
int subgraph_id = 0;


void dfs(int v)
{
    visiting.push(v);
    discovered[v] = true;
    belong[v] = ++subgraph_id;

    while (!visiting.empty()) {
        int now = visiting.top();
        visiting.pop();


        for (vector<int>::iterator it = vertexes[now].begin(); it != vertexes[now].end(); it++) {
            if (!discovered[*it]) {
                discovered[*it] = true;
                belong[*it] = subgraph_id;
                visiting.push(*it);
            }
        }
    }
}


int main()
{
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        vertexes[x].push_back(y);
        vertexes[y].push_back(x);
    }

    for (int i = 0; i < n; i++)
        if (!discovered[i])
            dfs(i);

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x, y;
        cin >> x >> y;
        if (belong[x] == belong[y])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

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

using namespace std;

#define ll long long
typedef complex<double> P;

int main() {
  int n, m; cin >> n >> m;
  int adj[n][n] = {};

  for (int i = 0; i < m; i++) {
    int v, u; cin >> v >> u;
    adj[v][u] = adj[u][v] = 1;
  }

  int q; cin >> q;
  int sd[n] = {};

  for (int i = 0; i < q; i++) {
    int s, t; cin >> s >> t;
    fill(sd, sd+n, 0);

    queue<int> que;
    que.push(s);
    bool f = false;

    while(!que.empty()) {
      int v = que.front(); que.pop();
      if(v == t) {
        f = true;
        break;
      }

      for (int j = 0; j < n; j++) {
        if(adj[v][j] == 1 && sd[j] == 0) {
          que.push(j);
        }
      }
    }

    cout << (f ? "yes" : "no") << endl;

  }
}

C++: #include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <utility>
#include <iostream>
#include <algorithm>
#include <functional>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define ll long long
#define ull unsigned long long
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define MOD7 1000000007
#define MOD9 1000000009
#define eps 1e-9
#define P pair<int, int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pul pair<ull, ull>
#define all(a) (a).begin(), (a).end()
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);--i)
#define REP(i,n) for (int i=0;i<(n);++i)
#define RREP(i,n) for (int i=(n)-1;i>=0;--i)

void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void yes() { cout << "yes" << endl; }
void no() { cout << "no" << endl; }

template<class T>
class DirectedGraph {
public:
	class Edge {
	public:
		int to;
		T cost;

		Edge(int t, T c) : to(t), cost(c) {}

		bool operator<(const Edge& right) const {
			return cost == right.cost ? to < right.to : cost < right.cost;
		}
	};

	vector<vector<Edge>> G;
	int V;

	DirectedGraph(int n = 0) {
		resize(n);
	}

	void resize(int n) {
		V = n;
		G.resize(n);
	}

	void addEdge(int from, int to, T cost = 0) {
		int v = max(from, to);
		if (v >= V)
			resize(v + 1);
		G[from].push_back(Edge(to, cost));
	}

	int col = 0;
	vector<int> visit;
	void dfs(int s) {
		//vector<int> visit(V);
		visit.resize(V);
		col++;
		calcDfs(visit, s);
	}

	void calcDfs(vector<int>& visit, int s) {
		if (visit[s])
			return;

		visit[s] = col;
		for (int i = 0; i < G[s].size(); ++i) {
			calcDfs(visit, G[s][i].to);
		}
	}

	vector<int> bfs(int s) {
		vector<int> dist(V);
		queue<int> que;

		fill(dist.begin(), dist.end(), -1);
		dist[s] = 0;
		que.push(s);
		while (!que.empty()) {
			int from = que.front();
			que.pop();
			for (int i = 0; i < G[from].size(); ++i) {
				int to = G[from][i].to;
				if (dist[to] != -1)
					continue;
				dist[to] = dist[from] + 1;
				que.push(to);
			}
		}

		return dist;
	}
};

int main() {
	DirectedGraph<int> g;
	int n, m, from, to;
	cin >> n >> m;
	while (m--) {
		cin >> from >> to;
		g.addEdge(from, to);
	}
	
	for (int i = 0; i < n; ++i)
		g.dfs(i);

	int q;
	cin >> q;
	while (q--) {
		cin >> from >> to;
		if (g.visit[from] == g.visit[to])
			yes();
		else
			no();
	}

	return 0;
}