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 <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>
#include <numeric>
#include <queue>

using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if ( i + 1 != list.size() ) {
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


class Node{
public:
    Node(int id) : id(id) 
    {
        // constructer
    }
    void AddFriend(int id){
        this->friends.push_back(id);
    }
    vector<int> Friends() {
        return this->friends;
    }

    int id;
    // bool done = false;
    int clazz = -1;
    vector<int> friends;
};

class List{
public:
    List(int numNode)
    {
        // constructer
        for (int i = 0; i < numNode; i++) {
            this->nodes.insert(std::make_pair(i, Node(i)));
        }
    }
    void SetRelation(int lhs, int rhs)
    {
        this->nodes.at(lhs).AddFriend(rhs);
        this->nodes.at(rhs).AddFriend(lhs);
    }
    int clazz = 1;
    void Traverse(){
        for(int i = 0; i < this->nodes.size(); i++) {
            this->_traverse(i);
            clazz++;
        }
    }
    void _traverse(int id){
        // printf("id %d'%c'\n", id, this->nodes.at(id).clazz);
        if (0 <= this->nodes.at(id).clazz) {
            return;
        }
        this->nodes.at(id).clazz = this->clazz;
        for (auto i : this->nodes.at(id).Friends()) {
            _traverse(i);
        }
    }
    bool Find(int from, int to)
    {
        // printf("find %d - %d\n", from, to);
        // printf("    '%d' : '%d'\n", this->nodes.at(from).clazz, this->nodes.at(to).clazz);
        if(this->nodes.at(from).clazz == -1 || this->nodes.at(to).clazz == -1) return false;
        return this->nodes.at(from).clazz == this->nodes.at(to).clazz;
    }
    // bool _find(int id){
    //     while(!this->queue.empty()){
    //         int i = queue.front();
    //         queue.pop();
    //         if (this->nodes.at(i).done) continue;
    //         for (auto relId : this->nodes.at(i).Friends()) {
    //             if (relId == id) {
    //                 return true;
    //             } else {
    //                 queue.push(relId);
    //             }
    //         }
    //         this->nodes.at(i).done = true;
    //     }
    //     return false;
    // }
    void Print() {
        for (int i = 1; i < nodes.size(); i++) {
            printf("%d %c\n", i, nodes.at(i).clazz);
        }
    }
private:
    std::unordered_map<int, Node> nodes;
    std::queue<int> queue;
};

void solve() {

    int n;
    scanf("%d", &n);
    List list(n);

    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        int lhs, rhs;
        scanf("%d %d", &lhs, &rhs);
        list.SetRelation(lhs, rhs);
    }
    list.Traverse();
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; i++) {
        int from, to;
        scanf("%d %d", &from, &to);
        if (list.Find(from, to)) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    return 0;
}

C++: #include <iostream>

using namespace std;

const int N = 100000;
int A[N][N] = {{0}};
int n;

bool dfs(int s, int t) {
    if (A[s][t] == 1) {
        return true;
    }
    for (int i = 0; i < n; ++i) {
        if (i != t && A[s][i] != 0) {
            if (dfs(i, t)) {
                return true;
            }
        }
    }
    return false;
}

int main() {
    int m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s][t] = 1;
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<stack>
#include<string>
using namespace std;

vector< vector<int> > L(100005);
int c[100005]={};
bool r;

void dfs (int v, int marker){
	if(c[v]!=0){
		r=false;
		return;
	}
	r=true;
	c[v]=marker;
	for (int i=0;i<L[v].size();i++){
		dfs(L[v][i],marker);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m,a,b,q,s,t;
	cin >> n >> m;
	
	for (int i=0;i<m;i++){
		cin >> a >> b;
		L[a].push_back(b);
	}
	
	int marker=1;
	for (int i=0;i<n;i++){
		dfs(i,marker);
		if (r) marker++;
	}
	
	
	for (int i=0;i<n;i++){
		cout << c[i] <<endl;
	}
	
	cin >> q;
	for (int i=0;i<q;i++){
		cin >> s >> t;
		if(c[s]==c[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	return 0;
}