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 <cstdio>
#include <vector>
#include <string>
#define N 100

void input_graph(std::vector<std::vector<int> >& g, int m){
    int s, t;
    for(int i=0; i<m; i++){//双連結リストを作る
        std::cin >> s >> t;
        g[s].push_back(t);
        g[t].push_back(s);
    }
}

void result(std::string ans[], int q){
    for(int i=0; i<q; i++){
        std::cout << ans[i] << '\n';
    }
}

void CC(std::vector<std::vector<int> >& g, std::string ans[], int q, int n){
    int s, t;
    int cnt = 0;
    std::vector<int> tmp;   //軌跡を保存
    //深さ優先探索
    while(cnt < q){
        //std::cout << "OK" << cnt << '\n';
        std::cin >> s >> t;
        int i = s;
        int nexti;
        int j;
        bool flag[n] = {false};
        bool f = false; //false:未到達 true:到達or終了
        while(!f){
            while(i != -1){
                //std::cout << "start\n";
                tmp.push_back(i);
                //std::cout << "i:" << i << '\n';
                flag[i] = true;
                //次の葉を探す
                if((int)g[i].size() != 0){//葉が最深でないとき
                    j = 0;
                    nexti = g[i][j];
                    while(flag[nexti]){
                        j++;
                        if(j == (int)g[i].size()){//葉がすべて探索済みなら
                            i = -1; //2つ目のwhile文を抜ける
                            break;
                        }
                        nexti = g[i][j];
                    }
                    if(i != -1){
                        if(nexti == t){
                            ans[cnt] = "yes";
                            i = -1;
                            f = true;
                            break;
                        }
                        i = nexti;
                    }
                }
                else{//葉が最深のとき
                    i = -1;
                }
            }
            if(!f){//まだ到達していなければ
                //ここから根に向かって戻る. 未探索の葉があればそちらへ行く
                //std::cout << "back\n";
                tmp.pop_back();
                i = (int)tmp.back();
                //std::cout << "i:" << i << '\n';
                tmp.pop_back();
                j = 0;
                nexti = g[i][j];
                while(flag[nexti]){
                    j++;
                    if(j == (int)g[i].size()){//現在のノードから未探索の葉がない
                        if((int)tmp.size() != 0){//まだtmpに軌跡が残っているなら
                            i = (int)tmp.back();
                            //std::cout << "i:" << i << '\n';
                            tmp.pop_back();
                            j = 0;
                            nexti = g[i][j];
                        }
                        else{//tmpに軌跡が残っていないなら
                            ans[cnt] = "no";
                            f = true;
                            break;
                        }
                    }
                }
            }
            i = nexti;
        }
        tmp.clear();    //一応clear
        cnt++;          //次のquestionへ
    }
}

int main(){
    int n, m;
    std::cin >> n >> m;
    std::vector<std::vector<int> > g;
    g.resize(n);
    input_graph(g, m);
    int q;
    std::cin >> q;          //質問数
    std::string ans[q];     //yes or no
    CC(g, ans, q, n);
    result(ans, q);
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
//#include "../cxx-prettyprint/prettyprint.hpp"

std::vector<std::vector<int>> Adj;
std::unordered_set<int> nodes_unvisited;
std::unordered_map<int, int> node_component_map;

void mark_nodes(int node, int component_id) {
    node_component_map[node] = component_id;
    nodes_unvisited.erase(node);
    for (int& next : Adj[node]) {
        if (nodes_unvisited.find(next) != nodes_unvisited.end()) {
            mark_nodes(next, component_id);
        }
    }
}

int main() {
    int n, m;
    std::cin >> n >> m;

    Adj.assign(n, std::vector<int>());
    for (int i = 0; i < m; i++) {
        int s, t;
        std::cin >> s >> t;
        Adj[s].push_back(t);
        Adj[t].push_back(s);
    }

    for (int i = 0; i < n; i ++) {
        nodes_unvisited.insert(i);
    }

    int component_id = 0;
    while (!nodes_unvisited.empty()) {
        int start = *nodes_unvisited.begin();
        mark_nodes(start, component_id);
        component_id ++;
    }

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

C++: #include <cstdio>
#include <vector>
#include <queue>
using namespace std;
static const int NIL = -1;

int n, m;
int *color;
vector<int> *v;

void bfs(int r, int c) {
    queue<int> q;
    q.push(r);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        if (color[u] == NIL) {
            color[u] = c;
        }
        for (vector<int>::iterator it = v[u].begin(); it != v[u].end(); it++) {
            if (color[*it] == NIL) {
                q.push(*it);
            }
        }
    }
}

void assignColor() {
    color = new int[n];
    int id = 0;
    for (int i = 0; i < n; i++) {
        color[i] = NIL;
    }
    for (int u = 0; u < n; u++) {
        if (color[u] == NIL) {
            bfs(u, id++);
        }
    }
}

int main() {
    scanf("%d %d", &n, &m);
    v = new vector<int>[n];

    for (int i = 0; i < m; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        v[s].push_back(t);
        v[t].push_back(s);
    }

    assignColor();

    int q;
    scanf("%d", &q);

    for (int i = 0; i < q; i++) {
        int s, t;
        scanf("%d %d", &s, &t);
        printf("%s\n", color[s] == color[t] ? "yes" : "no");
    }

    return 0;
}