Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c) {
    stack<int> S;
    S.push(r);
    color[r] = c;
    while ( !S.empty() ) {
        int u = S.top();
        S.pop();
        for ( int i = 0; i < G[u].size(); i++ ){
            int v = G[u][i];
            if ( color[v] == NIL ) {
                color[v] = c; S.push(v);
            }
            
        }
    }
        }

void assignColor() { int id = 1;
    for(int i=0; i<n; i++) color[i]=NIL;
    for(int u=0; u<n; u++){
        if(color[u] ==NIL) dfs(u,id++);
    }
    }

int main() {
    int s, t, m, q;
    cin >> n >> m;

    for ( int i = 0; i<m; i++){
         cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
         }
    assignColor();
    cin >> q;
    
         for ( int i = 0; i < q; i++ ) {
             cin >> s >> t;
             if ( color[s] == color[t] ) {
                 cout << "yes" << endl;
                 
             } else {
                     cout << "no" << endl;
                 
             }
         }
         return 0; }

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <list>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

int n,color[MAX];
vector<int>G[MAX];

void assignColor();
void dfs(int r, int c);

int main() {

	int s, t, m, q;
	cin >> n >> m;

	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}

	assignColor();

	cin >> q;

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

	return 0;
}

void assignColor() {
	int id = 1;
	for (int i = 0; i < n; i++) {
		color[i] = NIL;
	}
	for (int u = 0; u < n; u++) {
		if (color[u] == NIL)
			dfs(u, id++);
	}
		
}

void dfs(int r, int c) {
	stack<int>S;
	S.push(r);
	color[r] = c;

	while (!S.empty()) {
		int u = S.top();
		S.pop();
		for (int i = 0; i < G[u].size(); i++) {
			int v = G[u][i];
			if (color[v] == NIL) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}