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 <vector>
#include <stack>

using namespace std;

static const int MAX = 100000;
static const int NIL = -1;

int n; //number of users
vector<int> G[MAX];
int color[MAX];

void dfs(int userid, int groupid) {
    stack<int> S;
    S.push(userid);
    color[userid] = groupid;
    while ( !S.empty() ) {
        int user = S.top(); S.pop();
        for (unsigned long i = 0; i < G[user].size(); i++) {
            int partner = G[user][i];
            //if partner is not member of a group -> join to user's group
            if (color[partner] == NIL) {
                color[partner] = groupid;
                //add partner to stack, then check partner's partner list (use while)
                S.push(partner);
            }
        }
    }
}

//create a group list named as color
//result is color = [1, 1, 1, 2, 2, 3, 3, 3, 3, 3]
void assingColor() {
    int id = 1;
    for (int i = 0; i < n; i++) color[i] = NIL; // create empty group list
    for (int u = 0; u < n; u++) {
        //u = user id, id = group id
        if (color[u] == NIL) dfs(u, id++); //fill group list with dfs
    }
    //for (int i = 0; i < n; i++) cout << color[i] << " ";
    //cout << endl;
}

int main()
{
    int m; //number of relationships
    int s, t; //user and partner id
    int q; //number of question
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t); //push user's partner id to user relationship list
        G[t].push_back(s); //push user id to user's partner relationship list
    }
    assingColor();

    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> s >> t; // user and partner data
        if (color[s] == color[t]) { cout << "yes" << endl; }
        else { cout << "no" << endl; }
    }
    return 0;
}

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

using namespace std;

typedef long long ll;

const int INF = 1e9;

vector<vector<int> > g;
vector<int> ids;

void rec(int v, int id)
{
  //fprintf(stderr, "DEBUG: v=%d, id=%d\n", v, id);
  int n = g[v].size();
  ids[v] = id;
  for (int i = 0; i < n; ++i) {
    if (ids[g[v][i]] == -1) {
      rec(g[v][i], id);
    }
  }
}

int main()
{
  int n, m;
  fscanf(stdin, "%d %d", &n, &m);
  g = vector<vector<int> >(n);
  ids = vector<int>(n, -1);
  for (int i = 0; i < m; ++i) {
    int s, t;
    fscanf(stdin, "%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }
  int id = 0;
  for (int i = 0; i < n; ++i) {
    if (ids[i] == -1) {
      rec(i, id);
      ++id;
    }
  }
  int q;
  fscanf(stdin, "%d", &q);
  // fprintf(stderr, "DEBUG: ");
  // copy(ids.begin(), ids.end(), ostream_iterator<int>(cerr, ", "));
  // fprintf(stderr, "\n");
  for (int i = 0; i < q; ++i) {
    int s, t;
    fscanf(stdin, "%d %d", &s, &t);
    fprintf(stdout, "%s\n", ids[s] == ids[t] ? "yes" : "no");
  }
  return 0;
}

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

using namespace std;

const int N = 1e5 + 5;
int color[N];
vector<int> G[N];

void dfs(int u, int c) {
    color[u] = c;
    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (color[v] == -1) dfs(v, c);
    }
}

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

    memset(color, -1, sizeof(color));
    int c = 0;
    for (int u = 0; u < n; u++) {
        if (color[u] == -1) {
            dfs(u, c);
            c++;
        }
    }

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

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

const int N = 100000;
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int NIL = -1;

int n, M[N][N];
int color[N], cc[N], cp = 0;
int nt[N];

int next(int u){
	for (int v = nt[u]; v <= n; v++){
		nt[u] = v + 1;
		if (M[u][v]) return v;
	}
	return NIL;
}

void dfs_visit(int r){
	for (int i = 0; i < n; i++) nt[i] = 0;

	stack<int> S;
	S.push(r);
	color[r] = GRAY;
	cc[r] = cp;

	while (!S.empty()){
		int u = S.top();
		int v = next(u);
		if (v != NIL){
			if (color[v] == WHITE){
				color[v] = GRAY;
				cc[v] = cp;
				S.push(v);
			}
		}
		else {
			S.pop();
			color[u] = BLACK;
		}
	}
	cp++;
}

void dfs(){
	for (int i = 0; i < n; i++){
		color[i] = WHITE;
		nt[i] = 0;
	}

	for (int u = 0; u < n; u++){
		if (color[u] == WHITE) dfs_visit(u);
	}

}

int main(){
	int m, q;
	cin >> n >> m;
	for (int i = 0; i < n; i++){
		for (int j = 0; j < n; j++) M[i][j] = 0;
	}
	for (int i = 0; i < m; i++){
		int s, t;
		cin >> s >> t;
		M[s][t] = M[t][s] = 1;
	}

	dfs();

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

	return 0;
}