Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

struct node {
	int id;
	vector<int> rel;
};

int n, m, q;
node* nodes;

bool canReach(int s, int d, bool* h){
	h[s] = true;
	for(int i = 0; i < nodes[s].rel.size(); i++){
		if(nodes[s].rel[i] == d) return true;
		else if(!h[nodes[s].rel[i]]){
			if(canReach(nodes[s].rel[i], d, h)) return true;
		}
	}
	return false;
}

int main() {
	scanf("%d %d", &n, &m);
	
	nodes = new node[n];
	for(int i = 0; i < n; i++){
		nodes[i].id = i;
	}
	while(m--){
		int l, r;
		scanf("%d %d", &l, &r);
		nodes[l].rel.push_back(r);
		nodes[r].rel.push_back(l);
	}
	
	scanf("%d", &q);
	while(q--){
		int l, r;
		scanf("%d %d", &l, &r);
		
		bool *h = new bool[n];
		for(int i = 0; i < n; i++){
			h[i] = false;
		}
		printf("%s\n", canReach(l, r, h) ? "yes" : "no");
		delete[] h;
	}
	return 0;
}

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<int>& accesibleNodeSearch(const vector<vector<int>>& graphList
	, unordered_map<int, vector<int>>& visitMatrix
	, const int s
	, vector<bool>& visitList) 
{
	pair<unordered_map<int, vector<int>>::iterator, bool> result = visitMatrix.emplace(s,vector<int>());
	vector<int>& visitIDList = result.first->second;
	if (result.second) {//未計算の場合のみ計算する
		const size_t n = graphList.size();
		queue<int> nodeQueue;
		visitList.resize(n, false);
		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();
		}
		const int num = count(visitList.begin(), visitList.end(), true);
		visitIDList.reserve(num);
		for (int i = 0; i != n; ++i) {
			if (!visitList.at(i))continue;
			visitIDList.push_back(i);
		}
	}
	return visitIDList;
}

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

int main()
{


	execute(cin);



	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
const int num = 100001;
vector<int> v[num];
int n;
int co[num];
inline void dfs(int t, int c) {
	stack<int> st;
	st.push(t);
	co[t] = c;
	while (!st.empty()) {
		int u = st.top(); st.pop();
		for (int i = 0; i < v[u].size(); i++) {
			int g = v[u][i];
			cout << g << endl;
			if (co[g] == 0) {
				co[g] = c;
				st.push(g);
			}
		}
	}
}
int main() {
	int m, s, t, q, id = 0, a, b;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		co[i] = 0;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	for (int i = 0; i < n; i++) {
		if (co[i] == 0) {
			id++;
			dfs(i, id);
		}
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> a >> b;
		if (co[a] == co[b])
			cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<deque>
#define NOT_VISITED -1
using namespace std;

int main(){
  vector<vector<int> > path;
  vector<int> visited;
  int n,m,q;

  cin >> n >> m;
  path.resize(n);
  visited.resize(n);
  for(int j=0;j<n;j++){
    visited[j]=NOT_VISITED;
  }
  for(int i=0;i<m;i++){
    int s,t;

    cin >> s >> t;
    path[s].push_back(t);
    path[t].push_back(s);
  }

  cin >> q;
  for(int i=0;i<q;i++){
    deque<int> que;
    int s,t;
    
    cin >> s >> t;
    if(visited[s]==NOT_VISITED){
      visited[s]=s;
      que.push_back(s);
      while(!que.empty()){
        int myplace=que.front();

        que.pop_front();
        for(int j=0;j<path[myplace].size();j++){
          int to=path[myplace][j];
	
	  if(visited[to]==NOT_VISITED){
            visited[to]=s;
	    que.push_back(to);
	  }
        }
      }
    }

    if(visited[s]==visited[t]){
      cout << "yes" << endl;
    }else{
      cout << "no" << endl;
    }
  }
}