Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	for i := 0; i < n; i++ {
		group[i] = i
	}
	
	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())

		if group[s] < group[t] {
			group[t] = group[s]
		} else {
			group[s] = group[t]
		}
	}

	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 root(s, &group) == root(t, &group) {
			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 <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++);
    }
}