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<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;
    }
}