Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	fmt.Scan(&m)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < m; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())
	for i := 0; i < q; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())

		if group[s] == group[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func root(i int, group *[]int) int {
	if i == (*group)[i] {
		return i
	}
	(*group)[i] = root((*group)[i], group)
	return (*group)[i]
}

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 <bits/stdc++.h>
using namespace std;

void dfs(int in, int color,
    const vector<vector<int>>& graph,
    vector<int>& colors) 
{
    vector<bool> checkList(colors.size());
    stack<int> stack;
    stack.push(in);
    colors[in] = color;

    while (!stack.empty())
    {
        int x = stack.top();
        bool canLeave = true;
        for (const auto& g : graph[x]) {
            if (colors[g] == 0) {
                stack.push(g);
                colors[g] = color;
                checkList[g] = true;
                canLeave = false;
            }
        }
        if (canLeave) {
            stack.pop();
        }
    }
}

void wfs(int in, int color,
    const vector<vector<int>>& graph,
    vector<int>& colors)
{
    queue<int> queue;
    queue.push(in);
    vector<bool> checkList(colors.size());
    while (!queue.empty()) 
    {
        int x = queue.front();
        queue.pop();
        colors[x] = color;
        for (auto y : graph[x]) {
            if (!checkList[y]) {
                queue.push(y);
                checkList[y] = true;
            }
        }
    }
}

int main() {
    int n;
    int m;
    cin >> n >> m;
    vector<vector<int>> tree(n);
    vector<int> colors(n);

    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        tree[s].push_back(t);
        tree[t].push_back(s);
    }
    int color = 0;
    for (int i = 0; i < n; ++i) {
        if (colors[i] != 0) {
            continue;
        }
        wfs(i, ++color, tree, colors);
        //dfs(i, ++color, tree, colors);
    }

    int qNum;
    cin >> qNum;
    vector<pair<int, int>> question(qNum);
    for (auto& q : question) {
        cin >> q.first >> q.second;
    }

    for (const auto& q : question) {
        if (colors[q.first] == colors[q.second]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
}