Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<stack>
using namespace std;
const int MAXN = 100000 + 7;
const int NIL = -1;

vector<int> G[MAXN];
int status[MAXN];
int N;

void dfs( int  r, int c )
{
    stack<int> S;
    S.push(r);
    status[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 ( status[v] == NIL ) {
                status[v] = c;
                S.push(v);
            }
        }
    }
}

void set_status()
{
    int id = 1;
    memset( status, -1, sizeof(status) );
    for ( int u = 0; u < N; u++ ) {
        if ( status[u] == NIL )
            dfs( u, id++ );
    }
}

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

    set_status();

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

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;

struct UnionFind
{
	// par[i]????????????i????±?????????¨??????????????????i == par[i]?????¨???????????????i?????¨?????????????????§??????
	// sizes[i]???????????????i?????¨?????????????????????????????°???i?????????????????§????????´???????????????????????¨??????
	vector<int> par;
	vector<int> sizes;

	UnionFind(int n) : par(n), sizes(n, 1) {
		rep(i, n) par[i] = i;
	}

	int find(int x) {
		if (x == par[x]) return x;
		return par[x] = find(par[x]); // ????????????????????????????????°???????????????????????¢???
	}

	void unite(int x, int y) {
		x = find(x);
		y = find(y);

		if (x == y) return;

		// x?????¨???y?????¨????????§???????????????????????????
		if (sizes[x] < sizes[y]) swap(x, y);

		par[y] = x; // ??????????????????????????°??????
		sizes[x] += sizes[y];
		sizes[y] = 0;  // sizes[y]????????????????????¨???????????§0?????\????????????????????????
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}

	int size(int x) {
		return sizes[find(x)];
	}
};

int N, M, Q;

int main() {

	cin >> N >> M;
	UnionFind uf(N);
	rep(i, M){
		int f1, f2; cin >> f1 >> f2;
		uf.unite(f1, f2);
	}
	cin >> Q;
	rep(i, Q)
	{
		int f1, f2; cin >> f1 >> f2;
		cout << (uf.same(f1, f2) ? "yes" : "no") << endl;
	}

}

C++: #include <iostream>
#include <queue>
using namespace std;
struct Node{
    vector<int> fri;
    bool flag;
};
queue<Node> refresh(queue<Node> que,Node node[],int n){
    while(!que.empty())
        que.pop();
    
    for(int i=0; i<n; i++)
        node[i].flag=false;

    return que;
}

int main(){
    int n,m,s,t;
    Node node[100000];
    cin >> n >> m;

    for(int i=0; i<m; i++){
        cin >> s >> t;

        node[s].fri.push_back(t);
        node[t].fri.push_back(s);
    }

    queue<Node> que;
    refresh(que,node,n);

    int q;
    cin >> q;

    bool answer[10000];
    for(int i=0; i<q; i++){
        answer[i]=false;
        cin >> s >> t;

        node[s].flag=true;
        que.push(node[s]);

        while(!que.empty() && !answer[i]){
         Node fro = que.front();

         for(int j=0; j<fro.fri.size(); j++){
            int x = fro.fri[j];
  
            if(!node[x].flag){
                node[x].flag=true;
                que.push(node[x]);

		if(x==t){
		  answer[i]=true;
		  break;
		}
            }
         }

          que.pop();
        }

        que=refresh(que,node,n);
    }

    for(int i=0; i<q; i++){
        if(answer[i])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}

C++: /*
 * Problem Description: Connected Components
 * Category: Graph I
 * Author: Khan
 * Date: 21th October, 2017
 */

#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 i = 0; i < n; ++i)
        if(color[i] == NIL)
            DFS(i, id++);
}

int main(int argc, char const *argv[])
{
    //freopen("sample.txt", "r", stdin);
    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;
}