Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

int n,m,q,from,to;
int rank[100000],parent[100000];

int find(int x){
	if(x == parent[x])return x;
	else{
		return parent[x] = find(parent[x]);
	}
}

bool isSame(int x,int y){
	return find(x) == find(y);
}

void unite(int x,int y){
	x = find(x);
	y = find(y);

	if(x == y)return;

	if(rank[x] < rank[y]){
		parent[x] = y;
	}else{
		parent[y] = x;
		if(rank[x] == rank[y])rank[x]++;
	}
}

int main(){

	scanf("%d %d",&n,&m);

	for(int i = 0; i < n; i++){
		rank[i] = 0;
		parent[i] = i;
	}

	for(int i = 0; i < m; i++){
		scanf("%d %d",&from,&to);

		if(isSame(from,to))continue;
		unite(from,to);
	}

	scanf("%d",&q);

	int left,right;

	for(int i = 0; i < q; i++){
		scanf("%d %d",&left,&right);

		if(isSame(left,right))printf("yes\n");
		else{
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <set>
using namespace std;

const int MAX_N = 100000;

int G[MAX_N];
set<int> A[MAX_N];

void assign_gid(int user_num) {
  int gid;
  queue<int> Q;

  for (int i = 0; i < user_num; i++)
    Q.push(i);

  G[0] = gid = 0;

  while (!Q.empty()) {
    int c = Q.front(); Q.pop();

    if (G[c] == -1) G[c] = ++gid;

    for (set<int>::iterator it = A[c].begin(); it != A[c].end(); it++) {
      if (G[*it] == -1) {
        G[*it] = gid;
        Q.push(*it);
      }
    }
  }
}

int main() {
  int n, m; cin >> n >> m;

  for (int i = 0; i < n; i++) G[i] = -1;

  for (int i = 0; i < m; i++) {
    int s, d; cin >> s >> d;
    A[s].insert(d);
    A[d].insert(s);
  }

  assign_gid(n);

  int q; cin >> q;

  for (int i = 0; i < q; i++) {
    int s, d; cin >> s >> d;
    if (G[s] == G[d]) cout << "yes";
    else cout << "no";
    cout << endl;
  }

  return 0;
}

C++: //============================================================================
// Name        : Graph.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

struct Node{
	int id;
	vector<int> adjNodes;
	bool isFound;
	int discover_time;
	int finish_time;
	int distance;
	int group_id;
};

class Graph{
public:
	vector<Node> nodeList;
	map<int, vector<Node*> > distanceMap;
	Graph():nodeList(), distanceMap(){}
	Graph(int n):nodeList(), distanceMap(){
		for(int i=0; i<n; i++){
			vector<int> empty_vec;
			Node node = {i, empty_vec, false, -1, -1, -1, -1};
			nodeList.push_back(node);
		}
	}

	void insert(Node& i){
		nodeList.push_back(i);
	}

	void SearchDepthFirst(int source_id, int& timer, int group_id){
		Node& source_node = nodeList.at(source_id);
		source_node.isFound = true;
		source_node.discover_time = timer;
		for(int i=0; i<source_node.adjNodes.size(); i++){
			if(nodeList.at(source_node.adjNodes.at(i)).isFound == false){
				timer ++;
				nodeList.at(source_node.adjNodes.at(i)).isFound = true;
				nodeList.at(source_node.adjNodes.at(i)).discover_time = timer;
				SearchDepthFirst(source_node.adjNodes.at(i), timer, group_id);
				nodeList.at(source_node.adjNodes.at(i)).finish_time = timer;
				nodeList.at(source_node.adjNodes.at(i)).group_id = group_id;
			}
		}
		timer++;

		source_node.finish_time = timer;
	}

	void SearchDepthFirstAll(int& timer){
		int group_id = 0;
		for(int i=0; i<nodeList.size(); i++){
			if(nodeList.at(i).isFound == false){
				nodeList.at(i).group_id = group_id;
				SearchDepthFirst(i, timer, group_id);
				timer++;
				group_id++;
			}
		}
	}

	void writeSearchResult(){
		for(int i=0; i<nodeList.size(); i++){
			cout << nodeList.at(i).id << " " << nodeList.at(i).group_id << "\n";
		}
	}


	void addDistance(){
		vector<Node*> vec;
		vec.push_back(&nodeList.at(0));
		distanceMap[0] =vec;
		int cur_distance=0;

		while(cur_distance < nodeList.size()){
			vector<Node*>& vec_p_node = distanceMap[cur_distance];
			vector<Node*> empty_vec;
			distanceMap[cur_distance+1] = empty_vec;
			for(int i=0; i<vec_p_node.size(); i++){
				vector<int>& adjNodes = vec_p_node.at(i)->adjNodes;
				for(int j=0; j<adjNodes.size(); j++){
					if(nodeList.at(adjNodes.at(j)).distance == -1){
					    nodeList.at(adjNodes.at(j)).distance = cur_distance+1;
					    distanceMap[cur_distance+1].push_back(&nodeList.at(adjNodes.at(j)));
					}
				}
			}
			if(distanceMap[cur_distance+1].size() == 0){
				break;
			}

			cur_distance++;
		}
	}

	void writeDistance(){
		for(int i=0; i<nodeList.size(); i++){
			cout << i+1 << " " << nodeList.at(i).distance << "\n";
		}
	}

	void write(){
		for(int i=0; i<nodeList.size(); i++){
			Node& node = nodeList.at(i);
			cout << node.id << " " << node.adjNodes.size() << " ";

			for(int j=0; j<node.adjNodes.size(); j++){
				cout << node.adjNodes.at(j) << " ";
			}
			cout << "\n";
		}

	}

	void writeMap(){
		map<int, vector<Node*> >::iterator p;
		for(p=distanceMap.begin(); p!=distanceMap.end(); ++p){
			cout << "distance: " << p->first << ", id: ";
			vector<Node*>::iterator p2;
			for(p2=p->second.begin(); p2!=p->second.end(); ++p2){
				cout << (*p2)->id << " ";
			}
			cout << "\n";
		}
	}
};

void writeMatrix(vector<vector<int> > matrix){
	for(int i=0; i<matrix.size(); i++){
		for(int j=0; j<matrix.at(i).size()-1; j++){
			cout << matrix.at(i).at(j) << " ";
		}
		cout << matrix.at(i).at(matrix.at(i).size()-1);
		cout << "\n";
	}
}

int main() {
	int n, m;
	cin >> n >> m;
	Graph g(n);

	for(int i=0; i<m; i++){
		int id_1, id_2;
		cin >> id_1 >> id_2;
		g.nodeList.at(id_1).adjNodes.push_back(id_2);
		g.nodeList.at(id_2).adjNodes.push_back(id_1);
	}

	//g.write();
	int timer = 1;
	g.SearchDepthFirstAll(timer);
	//g.writeSearchResult();

	int q;
	cin >> q;

	for(int i=0; i<q; i++){
		int qid_1, qid_2;
		cin >> qid_1 >> qid_2;
		if(g.nodeList.at(qid_1).group_id == g.nodeList.at(qid_2).group_id){
			cout << "yes\n";
		}else{
			cout << "no\n";
		}
	}

	//g.nodeList.at(0).distance = 0;
	//g.addDistance();
	//g.writeDistance();

	return 0;
}