Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<set>
using namespace std;

bool DFS(vector<set<int> >& G, int a, int b) {
    if (G[a].size() == 0) return false;

    stack<pair<int, set<int>::iterator> > S;
    vector<bool> is_visited(G.size(), false);

    set<int>::iterator it = G[a].begin();
    do {
        if (it == G[a].end()) {
            int tmp = S.top().first;
            for (set<int>::iterator it2 = G[a].begin(); it2 != G[a].end(); it2++) {
                G[tmp].insert(*it2);
            }
            a   = S.top().first;
            it = S.top().second;
            S.pop();
            it++;
            continue;
        }
        if (*it == b) return true;
        if (!is_visited[*it]) {
            is_visited[*it] = true;
            if (G[a].find(b) != G[a].end()) return true;
            S.push(make_pair(a, it));
            a   = *it;
            it = G[a].begin();
            continue;
        }
        it++;
    } while (!S.empty());
    return false;
}


int main(){
    int n, m;
    cin >> n >> m;

    vector<set<int> > G(n);
// bool D[100000][100000];

    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        G[a].insert(b);
        G[b].insert(a);
    }

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (set<int>::iterator it = G[i].begin(); it != G[i].end(); it++){
//         cout << *it << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int id;
    node* parent;
};

int find(node x){
    if(x.parent == NULL)
        return x.id;
    else
        return find(*x.parent);
}

node person[100000];

void marge(node x, node y){
    int x_id = find(x);
    int y_id = find(y);
    if(x_id == y_id)
        return;
    else
        person[y_id].parent = &person[x_id];
    return;
}

bool judge(node x, node y){
    return find(x) == find(y);
}

int main() {
    cin >> n >> m;
    REP(i, n){
        person[i].id = i;
        person[i].parent = NULL;
    }
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[t].parent == NULL)
            person[t].parent = &person[s];
        else if(person[s].parent == NULL)
            person[s].parent = &person[t];
        else marge(person[s], person[t]);
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(judge(person[s], person[t]))
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

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

void dfsByStack(int s, int clr)
{
    stack<int> S;
    S.push(s);
    color[s] = clr;
    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] = clr;
                S.push(v);
            }
        }
    }
}

int main()
{
    int m, q, s, t;
    cin >> n >> m;
    for (int i = 0; i != m; ++i)
    {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    for (int i = 0; i != MAX; ++i)
    {
        color[i] = NIL;
    }
    int clr = 0;
    for (int i = 0; i != n; ++i)
    {
        if (color[i] == NIL)
        {
            dfsByStack(i, ++clr);
        }
    }
    cin >> m;
    for (int i = 0; i != m; ++i)
    {
        cin >> s >> t;
        if (color[s] == color[t])
        {
            cout << "yes" << endl;
        }
        else
        {
            cout << "no" << endl;
        }
    }
    return 0;
}