Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX = 100000
	NIL = -1
)

var (
	n, m, q, s, t 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() {
	sc.Split(bufio.ScanWords)
	n, m = nextInt(), nextInt()
	for i := 0; i < m; i++ {
		s, t = nextInt(), nextInt()
		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()
	q = nextInt()
	for i := 0; i < q; i++ {
		s, t = nextInt(), nextInt()
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}

}

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;
}

C++: #include<bits/stdc++.h>
using namespace std;

bool dfs(int node, int gole, vector<vector<int> >& adjlist, vector<bool>& used){

    if(node == gole) return true;

    for(int i = 0; i < adjlist[node].size(); i++){
        if(used[adjlist[node][i]] == false){
            used[adjlist[node][i]] = true;
            return dfs(adjlist[node][i], gole, adjlist, used);
        }
    }

    return false;
}

int main(){

    int n, m; cin >> n >> m;

    vector<vector<int> > adjlist(n);               

    //入力
    for(int i = 0; i < m; i++){
        int a, b; cin >> a >> b;                   // a ←→ b に辺がある
        adjlist[a].push_back(b);
        adjlist[b].push_back(a);
        
    }

    //質問
    int q; cin >> q;
    for(int i = 0; i < q; i++){
        int a, b; cin >> a >> b;
        
        vector<bool> used(n, false);
        used[a] = true;
        if(dfs(a, b, adjlist, used)) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;
}