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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int MAX = 100000;

vector<vector<int>> user(MAX, vector<int>(0));
vector<int> group(MAX, -1);

// 模範回答
// void dfs(int start, int group_i) {
//     stack<int> s;
//     group[start] = group_i;
//     s.push(start);
//     while (!s.empty()) {
//         int current = s.top();
//         s.pop();
//         for (int i = 0; i < user[current].size(); i++) {
//             int adj = user[current][i];
//             if (group[adj] == NIL) {
//                 s.push(adj);
//                 group[adj] = group_i;
//             }
//         }
//     }
// }

// 再帰によるdfs
void dfs(int start, int group_i) {
    group[start] = group_i;
    rep(i, user[start].size()) {
        int adj = user[start][i];
        if (group[adj] == -1) {
            dfs(adj, group_i);
        }
    }
}

// スタックによるdfsパターン1
// void dfs(int start, int group_i) {
//     stack<int> s;
//     group[start] = group_i;
//     s.push(start);
//     while (!s.empty()) {
//         int current = s.top();
//         group[current] = group_i;
//         bool stop = true;
//         for (int i = 0; i < user[current].size(); i++) {
//             int adj = user[current][i];
//             if (group[adj] == -1) {
//                 s.push(adj);
//                 stop = false;
//                 break;
//             }
//         }
//         if (stop) s.pop();
//     }
// }

int main() {
    int n, m, s, t, q;
    scanf("%d %d", &n, &m);
    rep(i, m) {
        scanf("%d %d", &s, &t);
        user[s].push_back(t);
        user[t].push_back(s);
    }

    int group_i = 0;
    rep(i, n) {
        if (group[i] == -1) {
            dfs(i, group_i++);
        }
    }

    scanf("%d", &q);
    rep(i, q) {
        scanf("%d %d", &s, &t);
        if (group[s] == group[t]) printf("yes\n");
        else printf("no\n");
    }
}

C++: #include <iostream>
#include <stack>
#include <vector>

using namespace std;

void dfs(vector<vector<int>>& g, vector<int>& color, int id, int c) {
    stack<int> s;
    vector<bool> visited(g.size(), false);
    s.push(id);
    color[id] = c;
    while (!s.empty()) {
        int id = s.top();
        s.pop();
        visited[id] = true;

        for (int i = 0; i < g[id].size(); ++i) {
            int adj = g[id][i];
            if (!visited[adj]) {
                color[adj] = c;
                s.push(adj);
            }
        }
    }
}

void assign_colors(vector<vector<int>>& g, vector<int>& color) {
    int color_id = 1;
    for (int i = 0; i < g.size(); ++i) {
        if (color[i] == -1) {
            dfs(g, color, i, color_id);
            ++color_id;
        }
    }
}

int main() {
    ios::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> g(n);
    for (int i = 0; i < m; ++i) {
        int p1, p2;
        cin >> p1 >> p2;
        g[p1].push_back(p2);
        g[p2].push_back(p1);
    }

    vector<int> color(n, -1);
    assign_colors(g, color);

    int p;
    cin >> p;
    for (int i = 0; i < p; ++i) {
        int p1, p2;
        cin >> p1 >> p2;
        if (color[p1] == color[p2])
            cout << "yes\n";
        else
            cout << "no\n";
    }
}

C++: #include <iostream>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
using namespace std;

int n;

vector<int> uf;

void initialize(int n)
{
  uf = vector<int>(n);
  for (int i = 0; i < n; i++)
    uf[i] = i;
}

int root(int i)
{
  if (uf[i] == i) return i;
  return uf[i] = root(uf[i]);
}

void unite(int i, int j)
{
  int ri = root(i);
  int rj = root(j);
  if (ri != rj)
    uf[rj] = ri;
}

bool same(int i, int j)
{
  return root(i) == root(j);
}

int main()
{
  int m, q;
  cin >> n >> m;

  initialize(n);
  for (int i = 0; i < m; i++) {
    int u, v; cin >> u >> v;
    unite(u, v);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    int u, v; cin >> u >> v;
    cout << (same(u, v)?"yes":"no") << endl;
  }

  return EXIT_SUCCESS;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
#include <stack>
#include <string>
#include <climits>
using namespace std;

#define MAX_N  100000
#define MAX_M  100000
#define MAX_Q  10000

int n, m, q, k, kind[MAX_N] = {};
queue<int> Q;

void bf(vector<vector<int> >& sns, int s)
{
    Q.push(s);
    while (!Q.empty()) {
        s = Q.front();
        Q.pop();
        kind[s] = k;
        for (int i = 0; i < sns[s].size(); i++) {
            if (!kind[sns[s][i]]) Q.push(sns[s][i]);
        }
    }
}

int main()
{
    cin >> n >> m;
    vector<vector<int> > sns(n);
    int s, d;
    while (m--) {
        cin >> s >> d;
        sns.at(s).push_back(d);
        sns.at(d).push_back(s);
    }
    k = 0;
    for (int s = 0; s < n; s++) {
        if (!kind[s]) {
            k++;
            bf(sns, s);
        }
    }
    cin >> q;
    while (q--) {
        cin >> s >> d;
        cout << (kind[s] == kind[d] ? "yes" : "no") << endl;
    }
    return 0;
}