Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

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

bool DFS(const vector<vector<int> >& G,
         vector<bool>& is_visited, int a, int b, int i) {
    is_visited[a] = true;
    if (a == b) return true;
    if (i >= G[a].size()) return false;
    if (!is_visited[G[a][i]]) {
        // is_visited[G[a][i]] = true;
        return DFS(G, is_visited, G[a][i], b, 0);
    }
    return DFS(G, is_visited, a, b, i + 1);
}

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

    vector<vector<int> > G(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

// for (int i = 0; i < n; i++) {
//     cout << i << " : ";
//     for (int j = 0; j < G[i].size(); j++){
//         cout << G[i][j] << " ";
//     }
//     cout << endl;
// }



    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        vector<bool> is_visited(n, false);
        if (DFS(G, is_visited, a, b, 0)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i< (n);i++)
using namespace std;
typedef long long ll;

struct BFS{
    vector<int> color;
    vector<vector<int>> adj;
    int n_,m_;
    BFS(int n,int m):color(n,-1),adj(n){
        n_ = n;
        m_ = m;
    }
    void init(){
        rep(i,m_){
            int a,b;cin>>a>>b;
            adj[a].push_back(b);
            adj[b].push_back(a);
        }
    }
    void visit(int u,int clr){
        queue<int> Q;
        Q.push(u);
        color[u] = clr;
        while(!Q.empty()){
            int v = Q.front();Q.pop();
            color[v] = clr;
            for(int h:adj[v]){
                if(color[h] == -1){
                    Q.push(h);
                }
            }
        }
    }
    void go(){
        int clr_ = 0;
        rep(i,n_){
            if(color[i] == -1){
                visit(i,clr_);
                clr_++;
            }
        }
    }
    void solve(){
        init();
        go();
        int q;cin>>q;
        rep(i,q){
            int a,b;cin>>a>>b;
            if(color[a] == color[b]){
                cout<<"yes"<<endl;
            }else{
                cout<<"no"<<endl;
            }
        }
    }
};

int main(){
    int n,m;cin>>n>>m;
    BFS bfs(n,m);
    bfs.solve();
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#define MAX_NODE 100000
#define MAX_EDGE 100000

using namespace std;

int n;//num of nodes
int e;//num of edges
int group[MAX_NODE+1];//0オリジン
int q;

bool neighbor_a[MAX_NODE+1];
vector<int> neighbor_v[MAX_NODE+1];//連接リスト

void addNeighbor(int num,int ori){//oriを含む連結グラフをつくる
	int ngb;
	for(int i = 0;i < neighbor_v[num].size();i++){//深さ優先探索
		if(neighbor_a[neighbor_v[num][i]] == false){
			neighbor_a[neighbor_v[num][i]] = true;
			neighbor_v[ori].push_back(neighbor_v[num][i]);
			ngb = neighbor_v[num][i];
			addNeighbor(ngb,ori);
		}
	}
}


int main(){
	int v_1,v_2;
	scanf("%d %d",&n,&e);

	for(int i = 0;i < e;i++){
		scanf("%d %d",&v_1,&v_2);
		neighbor_v[v_1].push_back(v_2);
		neighbor_v[v_2].push_back(v_1);
	}

	for(int i  = 0;i < n;i++) group[i] = -1;

	for(int i = 0;i < n;i++){
		if(group[i] != -1) continue;

		for(int j = 0;j < n;j++) neighbor_a[j] = false;//iの連接配列

		addNeighbor(i,i);

		for(int j = 0;j < neighbor_v[i].size();j++) group[neighbor_v[i][j]] = i;

	}
	//for(int i = 0;i < n;i++) cout <<group[i] <<" ";
	//for(int i = 0;i < neighbor_v[5].size();i++) cout <<neighbor_v[5][i] <<" ";

	scanf("%d",&q);

	for(int i = 0;i < q;i++){
		scanf("%d %d",&v_1,&v_2);
		if(group[v_1] == group[v_2]) cout <<"yes" <<endl;
		else cout <<"no" <<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 100000

struct Node{
	int group;
	queue<int> fr;
};

int N, M, Q, group_num = 0;
Node person[MAX];

void isFriend(int a, int b){
	if(person[a].group < 0 && person[b].group < 0){
		int p = a, next, m;
		queue<int> move;
		group_num++;
		person[p].group = group_num;
		//cout << a << " is group " << person[a].group << endl;
		move.push(p);

		while(!move.empty()){
			p = move.front();
			if(person[p].fr.empty()){
				move.pop();
			} else {
				m = person[p].fr.size();
				while(m--){
					next = person[p].fr.front();
					person[p].fr.pop();
					if(person[next].group < 0){
						person[next].group = group_num;
						//cout << next << " is group " << person[next].group << endl;
						move.push(next);
					}
				}
				move.pop();
			}
		}

		if(person[a].group == person[b].group){
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	} else if(person[a].group == person[b].group){
		cout << "yes" << endl;
	} else {
		cout << "no" << endl;
	}
}

int main(){
	cin >> N >> M;
	for(int i = 0; i < N; i++)
		person[i].group = -1;
	while(M--){
		int a, b;
		cin >> a >> b;
		person[a].fr.push(b);
		person[b].fr.push(a);
	}
	cin >> Q;
	while(Q--){
		int a, b;
		cin >> a >> b;
		isFriend(a, b);
	}
	return 0;
}

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

bool DFS(const vector<vector<int> >& G, int a, int b, int i) {
    if (a == b) return true;
    if (i >= G[a].size()) return false;
    return DFS(G, G[a][i], b, 0) || DFS(G, a, b, i + 1);
}

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

    vector<vector<int> > G(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        vector<vector<bool> > visited(false);
        if (DFS(G, a, b, 0)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}