Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

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