Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

C++: #include<iostream>
#include<vector>
#include<stack>
#define MAX 100000
#define NIL -1

// 隣接リスト
std::vector<int> G[MAX];

// 隣接する頂点に同じ色を付けて記録するデータ
int color[MAX];

// 頂点数
int n;

// 深さ探索
// 探索した頂点には隣接頂点の印としてcの色を付ける
void dfs(int r, int c)
{
	std::stack<int> S;
	S.push(r);
	color[r] = c;

	while (!S.empty())
	{
		int u = S.top();
		S.pop();
		for (int i = 0; i < G[u].size(); i++)
		{
			int v = G[u][i];
			if (color[v] == NIL)
			{
				color[v] = c;
				S.push(v);
			}
		}
	}
}

// 色を付けて隣接頂点同士に分類する
void assignColor()
{
	int id = 1;
	for (int i = 0; i < n; i++)color[i] = NIL;
	for (int u = 0; u < n; u++)
	{
		if (color[u] == NIL)dfs(u, id++);
	}
}

int main()
{
	int m;
	std::cin >> n >> 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);
	}

	assignColor();

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

	return 0;
}

// 参考
// ALDS1_11_B_DepthFirstSearchと同じロジックで深さ探索を実装
void dfs_2(int r, int c)
{
	std::stack<int> S;
	S.push(r);
	color[r] = c;
	while (!S.empty())
	{
		int u = S.top();
		int v = -1;
		for (int i = 0; i < G[u].size(); i++)
		{
			if (color[G[u][i]] == NIL)
			{
				v = G[u][i];
			}
		}
		if (v != -1)
		{
			S.push(v);
			color[v] = c;
		}
		else
		{
			S.pop();
		}
	}
}

C++: #include <stdio.h>
#include <stack>
#include <vector> 
#include <iostream>
#define N 100000
using namespace std;

vector<int>  G[N];
int color[N];
int n;

void dfs(int s, int c)
{
    stack<int> st;
    color[s] = c;
    st.push(s);
    while(!st.empty())
    {
        int u = st.top();
        st.pop();
        for (int i = 0; i < G[u].size(); i++)
        {
            int v = G[u][i];
            if(color[v] == -1)
            {
                color[v] = c;
                st.push(v);
            }
        } 
    }
    return;
}

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

    return;
}

int main()
{
    //freopen("in3.txt","r",stdin);
    int m;
    scanf("%d %d",&n, &m);

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

    assignColor(); 

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

C++: #include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
#define N 100000
#define WHITE 0
#define GRAY 1
#define BLACK 2

vector<int> G[100];
int color[N], num_members;
int cnt[N];

string dfs(int r, int target) {
    stack<int> S;
    S.push(r);
    color[r] = GRAY;

    while(!S.empty()) {
        int u = S.top();
        int v = G[u][cnt[u]];
        if(u==target) {
            return "yes";
        }
        cnt[u]++;

        if(v!=-1) {
            if(color[v]==WHITE) {
                color[v] = GRAY;
                S.push(v);
            }
        }
        else {
            S.pop();
            color[u] = BLACK;
        }
    }

    return "no";
}

int main() {
    int num_relations, from_id, to_id;
    int num_tests, test_from_id, test_to_id;

    cin >> num_members >> num_relations;
    for(int i=0; i<num_relations; i++) {
        cin >> from_id >> to_id;
        G[from_id].push_back(to_id);
    }
    //最後に番兵的な何かを仕込んでおく
    for(int i=0; i<num_members; i++) {
        G[i].push_back(-1);
    }

    cin >> num_tests;
    for(int i=0; i<num_tests; i++) {
        for(int j=0; j<num_members; j++) {
            color[j] = WHITE;
            cnt[j] = 0;
        }
        cin >> test_from_id >> test_to_id;
        cout << dfs(test_from_id, test_to_id) << endl;
    }
    
    return 0;
}

C++: //ALDS1_11_B:Depth First Search
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <random>
#include <time.h>
#include <cassert>
using namespace std;

vector<int> friends[100000];
int group[100000];

void dfs(int now, int g){
    group[now] = g;
    for(int i=0; i<friends[now].size(); i++){
        if(group[friends[now][i]] == -1) dfs(friends[now][i], g);
    }
}

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

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

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

C++: #include <iostream>
#include <vector>
#include <stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

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

void assignColor();
void dfs(int r, int c);

int main() {
    int s, t, m, q;

    cin >> n >> m;

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

    assignColor();

    cin >> q;

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

}


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

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