Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#define MAX 100000

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 << "\nOK" << 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();
                if(i > MAX){
                    ans[cnt] = "no";
                    f = true;
                    break;
                }
                //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>
#include<limits.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

#define MAX 100000

vector<vector<int>> adj_list(MAX);
vector<int> visited(MAX, -1);
vector<int> init_visited(MAX, -1);

int group = 0;

void dfs(int node){
    // cout << "init node " << node << endl;
    visited.at(node) = group;
    
    for(int i = 0; ; i++){
        int next_node = adj_list.at(node).at(i);
        // cout << next_node << endl;

        if(next_node != -1 && visited.at(next_node) == -1){
            // cout << "called dfs with " << next_node << endl;
            dfs(next_node);
        }
        if(next_node == -1){
            break;
        }
    }
    // cout << "returned from " << node << endl;
}

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


    //　隣接リストの作成
    rep(i, m){
        int from, to;
        cin >> from >> to;
        adj_list.at(from).push_back(to);
        adj_list.at(to).push_back(from);
    }
    // 番兵の挿入
    rep(i, n){
        adj_list.at(i).push_back(-1);
    }

    for(int i = 0; i < n; i++){
        if(visited.at(i) == -1){
            dfs(i);
            group++;
        }
    }

    int q; cin >> q;
    rep(i, q){
        int from, to;
        cin >> from >> to;
        if(visited.at(from) == visited.at(to)){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }

    // cout << "=================" << endl;

    // for(int i = 0; i < m; i++){
    //     cout << "node " << i << " ";
    //     for(int j = 0; j < adj_list.at(i).size(); j++){
    //         cout << adj_list.at(i).at(j) << " ";
    //     }
    //     cout << endl;
    // }

    // rep(i, n){
    //     cout << "node " << i << " " << visited.at(i) << endl;
    // }
}

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

const long MAX = 100000;
const long NIL = -1;

long n;
vector<long> G[MAX];
long color[MAX];

void dfs(long r, long c){
	stack<long> S;
	S.push(r);
	color[r] = c;
	while (!S.empty()){
		long u = S.top();
		S.pop();
		for (long i = 0; i < G[u].size(); i++){
			long v = G[u][i];
			if (color[v] == NIL){
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor(){
	long id = 1;
	for (long i = 0; i < n; i++) color[i] = NIL;
	for (long u = 0; u < n; u++){
		if (color[u] == NIL) dfs(u, id++);
	}
}

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

	assignColor();

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

	return 0;
}