Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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