Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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