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<stdio.h>
#include<queue>

#define MAX 100000

int G[MAX+1][MAX+1];
int n, m;
std::queue<int> Q;

bool bfs(int src, int dest) {
    bool C[MAX];
    for (int i = 0; i < n; ++i) C[i] = false;

    Q.push(src);
    while(!Q.empty()) {
        int cur = Q.front();Q.pop();
        for (int i = 0; i < n; ++i) {
            if (G[cur][i] == 0) continue;
            if (C[i]) continue;
            if (i == dest) return true;
            Q.push(i);
            C[i] = true;
        }
    }

    return false;
}

int main() {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            G[i][j] = 0;
        }
    }
    //for (int i = 0; i < m; ++i) {
    //    int u,k;
    //    scanf("%d %d", &u, &k);
    //    G[u][k] = 1;
    //    G[k][u] = 1;
    //}

    //int q;
    //scanf("%d", &q);
    //for (int i = 1; i <= q; ++i) {
    //    int s,d;
    //    scanf("%d %d", &s, &d);
    //    bfs(s, d) ? printf("yes\n") : printf("no\n");
    //}

    return 0;
}

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 dfs(int r, int c)
{
    // 双方向に行ける頂点の集団の色をcとして，rを始点とする
    stack<int> S;
    S.push(r);
    color[r] = c;
    while(!S.empty()) {
        // rから行けるところなくなるまで深さ優先探索する
        int u = S.top();
        S.pop();
        for (int i = 0; i < G[u].size(); i++) {  // G[u].size()はuが行く可能性のある頂点の数だけ
            int v = G[u][i];  // vはuが行く可能性のある頂点
            if (color[v] == NIL) {
                // 配列colorで行く可能性のある頂点の色が埋まってなかったら
                // 行く可能性のある頂点の色をc(= id)とする
                // そしてvをSに格納する
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assignColor()
{
    // 双方向に行ける集団の色を同じにする
    int id = 1;
    for (int i = 0; i < n; i++) {
        color[i] = NIL;  // 配列colorを初期化
    }
    for (int u = 0; u < n; u++) {
        // 初期化したcolorに対して，深さ優先探索をする
        if (color[u] == NIL) {
            dfs(u, id++);
        }
    }
}

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

    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        cin >> s >> t;
        // 有向グラフの隣接リスト
        // s->tとt->s
        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" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;
}

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 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++); //それぞれの連結な部分グラフに番号（色）を与える
    }
}

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" << endl;
        }else{
            cout << "no" << endl;
        }
    }

    return 0;
}

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

static const int MAX_NUM = 100000;

void DFS(int group[], vector<int> vec[], int gnum, int start)
{
    stack<int> S;
    int target;
    int i;
    int tmp;

    S.push(start);
    group[start] = gnum;

    while (S.empty() == false) {
        target = S.top();
        for (i = 0; i < vec[target].size(); i++) {
            if (group[vec[target][i]] == 0) {
                group[vec[target][i]] = gnum;
                S.push(vec[target][i]);
                break;
            }
        }
        if (i == vec[target].size()) {
            S.pop();
        }
    }

    return;
}

void SetupGroup(int group[], vector<int> vec[], int elemnum)
{
    int i;
    int gnum = 0;

    for (i = 0; i < elemnum; i++) {
        if (group[i] == 0) {
            ++gnum;
            DFS(group, vec, gnum, i);
        }
    }
}

int main(void)
{
    int i;
    int num;
    int elemnum;
    int qnum;
    int ele1, ele2;
    vector<int> vec[MAX_NUM];
    int group[MAX_NUM] = {0};

    scanf("%d %d", &elemnum, &num);
    for (i = 0; i < num; i++) {
        scanf("%d %d", &ele1, &ele2);
        vec[ele1].push_back(ele2);
        vec[ele2].push_back(ele1);
    }

    SetupGroup(group, vec, elemnum);

    scanf("%d", &qnum);
    for (i = 0; i < qnum; i++) {
        scanf("%d %d", &ele1, &ele2);
        if (group[ele1] == group[ele2]) {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
}

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

bool x[10000][10000];
bool y[10000];

int main(){
	int n,m,a,b,q;
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>a>>b;
		x[a][b]=true;
		x[b][a]=true;
	}
	cin>>q;
	for(int h=0;h<q;h++){
		cin>>a>>b;
		memset(y,0,sizeof(y));
		y[a]=true;
		for(int i=0;i<100;i++){
			for(int j=0;j<n;j++){
				if(y[j]==true){
					for(int k=0;k<n;k++){
						if(x[j][k]==true){
							y[k]=true;
						}
					}
				}
				if(y[b]==true){
					cout<<"yes"<<endl;
					goto Exit;
				}
			}
		}
		cout<<"no"<<endl;
		Exit:;
	}
	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxx=100010;

int pre[maxx];

int find(int u){
	while(pre[u]!=u) u=find(pre[u]);
	return u;
}

int main(){
	int n,m,x,y;
	cin>>n>>m;
	for(int i=0;i<n;i++) pre[i]=i;
//	for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
//	for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
	while(m--){
		cin>>x>>y;
		pre[find(x)]=find(y);
//		for(int i=0;i<n;i++) cout<<i<<" ";cout<<endl;
//		for(int i=0;i<n;i++) cout<<pre[i]<<" ";cout<<endl;
	}
	cin>>m;
	while(m--){
		cin>>x>>y;
		if(find(x)==find(y)) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}