Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

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

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 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;
}

C++: #include <iostream>
#include <vector>
using namespace std;
vector<int> G[100];
vector<int> A;
int isVisited[100];

bool enableConnection(int l, int o)
{
    if (isVisited[l] == 1) {
	return false;
    }
    //?¨???????????????????????????????????????????????????????
    for (int i = 0; i < G[l].size(); i++) {
	if (G[l][i] == o) {
	    return true;
	}
	if (enableConnection(G[l][i], o)) {
	    return true;
	}
    }
    return false;
}

int main()
{
    int a, c;
    cin >> a;
    cin >> c;
    //setup
    for (int i = 0; i < c; i++) {
	int n;
	int m;
	cin >> n >> m;
	G[n].push_back(m);
	isVisited[i] = 0;
    }

    //then
    int b = 0;
    cin >> b;
    for (int i = 0; i < b; i++) {
	int l;
	int o;
	cin >> l;
	cin >> o;
	if (enableConnection(l, o) || enableConnection(o, l)) {
	    cout << "yes" << endl;
	} else {
	    cout << "no" << endl;
	}
    }
    return 0;
}