Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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;
    int i, u, v;
    while ( !S.empty() ) {
        u = S.top();
        S.pop();
        for ( i = 0; i < G[u].size(); i++ ) {
            v = G[u][i];
            if ( color[v] == NIL ) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}
 
void assignColor() {
    int id = 0;
    int i, u;
    for ( i = 0; i < n; i++ ) color[i] = NIL;
    for ( u = 0; u < n; u++ ) {
        if ( color[u] == NIL ) dfs(u, id++);
    }
}
 
int main() {
    int s, t, m, q, i;
 
    cin >> n >> m;
 
    for ( i = 0; i < m; i++ ) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
 
    assignColor();
 
    cin >> q;
 
    for ( i = 0; i < q; i++ ) {
        cin >> s >> t;
        if ( color[s] == color[t] ) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
 
    return 0;
}