Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

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

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

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

C++: #include <bits/stdc++.h>
using namespace std;

#define NIL -1

void dfs(int r, int id, vector<vector<int>> &G, vector<int> &color) {
    stack<int> S;
    S.push(r);

    color[r] = id;
    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] = id;
                S.push(v);
            }
        }
    }
}

void assignColor(vector<vector<int>> &G, vector<int> &color) {
    int id = 1;

    for (int i = 0; i < G.size(); i++)
        color[i] = NIL;

    for (int u = 0; u < G.size(); u++) {
        if (color[u] == NIL) {
            dfs(u, id, G, color);
            id++;
        }
    }
}

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

    vector<vector<int>> G(n);
    vector<int> color(n);

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

    // グループ分け
    assignColor(G, color);

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

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1LL << 60;
const ll MOD = 1000000007;

int N, M;
int cnt = 0;
vector<vector<int>> G;

vector<int> seen;
void dfs(int v) {
    seen[v] = cnt;

    for (auto nv : G[v]) {
        if (seen[nv] != -1) continue;

        dfs(nv);
    }
}

int main() {
    cin >> N >> M;
    G.resize(N);
    seen.assign(N, -1);
    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 v = 0; v < N; v++) {
        if (seen[v] != -1) continue;

        dfs(v);
        cnt++;
    }

    int q;  cin >> q;
    for (int i = 0; i < q; i++) {
        int a, b; cin >> a >> b;
        if (seen[a] == seen[b]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
    return 0;
}

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

#define MAX_N 100000
#define MAX_M 100000

vector<int> G[MAX_N];

int main(void){
    int n, m;
    scanf("%d %d\n", &n, &m);
    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d %d", &a, &b);

        for(int j = 0; j < G[a].size(); j++){
            if(G[a][j] == b){
                printf("yes\n");
                break;
            }
            if(j == G[a].size() - 1){
                printf("no\n");
            }
        }
    }
}

C++: #include <stdio.h>
#include <set>
#define MAX_USER (100000)

typedef std::set<long> Circle;

void merge_circle( Circle** circleinfo , long uid1 , long uid2 )
{
	Circle* src;
	Circle* dst;
	if( circleinfo[ uid1 ]->size() < circleinfo[ uid2 ]->size() )
	{
		src = circleinfo[ uid1 ];
		dst = circleinfo[ uid2 ];
	}
	else
	{
		src = circleinfo[ uid2 ];
		dst = circleinfo[ uid1 ];
	}

	Circle::const_iterator itr;
	for( itr = src->begin() ; itr != src->end() ; itr++ )
	{
		circleinfo[ *itr ] = dst;
		dst->insert( *itr );
	}
}

void add_member( Circle** circleinfo , long uid1 , long uid2 )
{
	if( circleinfo[ uid1 ] == 0 && circleinfo[ uid2 ] == 0 )
	{
		//create new circle
		Circle* entry = new Circle;
		entry->insert( uid1 );
		entry->insert( uid2 );

		circleinfo[ uid1 ] = entry;
		circleinfo[ uid2 ] = entry;
	}
	else if( circleinfo[ uid1 ] != 0 && circleinfo[ uid2 ] == 0 )
	{
		circleinfo[ uid1 ]->insert( uid2 );
		circleinfo[ uid2 ] = circleinfo[ uid1 ];
	}
	else if( circleinfo[ uid1 ] == 0 && circleinfo[ uid2 ] != 0 )
	{
		circleinfo[ uid2 ]->insert( uid1 );
		circleinfo[ uid1 ] = circleinfo[ uid2 ];
	}
	else
	{
		if( circleinfo[ uid1 ] != circleinfo[ uid2 ] )
		{
			merge_circle( circleinfo , uid1 , uid2 );
		}
	}
}

int main( int argc , char** argv )
{

	//read setting
	long user_count;
	long friendship_count;
	scanf( "%ld %ld" , &user_count , &friendship_count );

	//initialize
	Circle** user_info = new Circle*[ user_count ];
	for( long i = 0 ; i < user_count ; i++ )
	{
		user_info[ i ] = 0;
	}

	//read user friendship
	for( long i = 0 ; i < friendship_count ; i++ )
	{
		long uid1;
		long uid2;
		scanf( "%ld %ld" , &uid1 , &uid2 );

		add_member( user_info , uid1 , uid2 );
	}

	//read number of question
	long question_count;
	scanf( "%ld" , &question_count );

	//process question
	for( long i = 0 ; i < question_count ; i++ )
	{
		long uid1;
		long uid2;
		scanf( "%ld %ld" , &uid1 , &uid2 );

		if( user_info[ uid1 ] == user_info[ uid2 ] && user_info[ uid1 ] != 0 )
		{
			printf( "yes\n" );
		}
		else
		{
			printf( "no\n" );
		}
	}

	delete[] user_info;
	return 0;
}

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

vector<int> ad[100000];
vector<int> v,d;

int main()
{
	int i,j,n,m,a,b,q,l,k;
	queue<int> Q;
	
	cin >> n;
	cin >> m;
	for(i=0;i<=m-1;i++)
	{
		cin >> a;
		cin >> b;
		ad[a].push_back(b);
		ad[b].push_back(a);
	}
	cin >> q;
	for(i=0;i<=q-1;i++)
	{
		cin >> a;
		cin >> b;
		for(j=0;j<=n-1;j++)
		{
			v.push_back(0);
			d.push_back(-1);
		}
		v[a]=1;
		d[a]=0;
		Q.push(a);
		while(Q.empty()!=1)
		{
			l=Q.front();
			for(k=0;k<=ad[l].size()-1;k++)
			{
				if(v[ad[l][k]]==0)
				{
				    Q.push(ad[l][k]);
				    v[ad[l][k]]=1;
					d[ad[l][k]]=d[l]+1;
				}
			}
			Q.pop();
		}
		if(d[b]>=0) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}