Go: package main

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

func main() {
	var n,m int
	fmt.Scan(&n)
	group := make([]int, n)
	g := make([][]int, n)
	for i := 0; i < n; i++ {
		g[i] = []int{}
		group[i] = -1
	}
	
	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())
		
		g[s] = append(g[s], t)
		g[t] = append(g[t], s)
	}
	for i := 0; i < n; i++ {
		if group[i] != -1 {
			continue
		}
		q := []int{i}
		for len(q) > 0 {
			pop := q[0]
			if len(q) > 1 {
				q = q[1:]
			} else {
				q = []int{}
			}
			if group[pop] == -1 {
				q = append(q, g[pop]...)
				group[pop] = i
			}
		}
	}
	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 group[s] == group[t] {
			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>

#define MAX_USER (100000)

/*

1)initial state

	0    1    2    3    4    5...

2)create friendship 
	friend: 0 and 1
	friend: 2 and 3

	0    2    4
    |    |
	1    3

3)and more
	friend: 2 and 4

	0       2      5
	|      / \
	1     3---4

*/



struct UserInfo
{
	long uid;
	UserInfo* next;
	UserInfo* prev;
};

int examine_friendship( const UserInfo* user_info , long uid1 , long uid2 )
{
	UserInfo* uid_ptr = user_info[ uid1 ].next;

	while( uid_ptr->uid != uid1 )
	{
		if( uid_ptr->uid == uid2 )
		{
			return 1;
		}
		uid_ptr = uid_ptr->next;
	}
	return 0;
}

int main( int argc , char** argv )
{
	//read setting
	long user_count;
	long friendship_count;
	scanf( "%ld %ld" , &user_count , &friendship_count );

	//initialize
	UserInfo*  user_info = new UserInfo[ user_count ];
	for( long i = 0 ; i < user_count ; i++ )
	{
		user_info[ i ].uid = i;
		user_info[ i ].next = &user_info[ i ];
		user_info[ i ].prev = &user_info[ i ];
	}

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

		//update internal data
		UserInfo* uid1_ptr = &user_info[ uid1 ];
		UserInfo* uid2_ptr = &user_info[ uid2 ];
		UserInfo* uid1_next_ptr = uid1_ptr->next;

		uid1_ptr->next = uid2_ptr;
		uid2_ptr->prev = uid1_ptr;

		uid1_next_ptr->prev = uid2_ptr;
		uid2_ptr->next = uid1_next_ptr;
	}

	//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( examine_friendship( user_info , uid1 ,uid2 ) )
		{
			printf( "yes\n" );
		}
		else
		{
			printf( "no\n" );
		}
	}

	return 0;
}

C++: #include<iostream>
#define MAX 100
using namespace std;

int main() {
	int i, n, m, q, a, b, gnum = 0, ptog[MAX] = {};

	cin >> n >> m;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		if (ptog[a] != 0)ptog[b] = ptog[a];
		else if (ptog[b] != 0)ptog[a] = ptog[b];
		else ptog[a] = ptog[b] = ++gnum;
	}

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> a >> b;
		if (ptog[a] == ptog[b])cout << "yes\n";
		else cout << "no\n";
	}
}

C++: #include <bits/stdc++.h>

#define int             long long
#define FOR( i, m, n )  for( int (i) = (m); (i) < (n); (i)++ )
#define REP( i, n )     FOR( i, 0, n )
#define REPR( i, m )    for( int (i) = (m); (i) >= 0; (i)-- )
#define REPONE( i, n )  FOR( i, 1, n + 1 )
#define ALL( a )        (a).begin(), (a).end()
#define MP              make_pair

using namespace std;
using P = pair<int, int>;

template<class T>bool chmax( T& a, const T& b ) { if( a < b ) { a = b; return 1; } return 0; }
template<class T>bool chmin( T& a, const T& b ) { if( a > b ) { a = b; return 1; } return 0; }

const int INF = 1e9;
const int MOD = 1e9 + 7;

vector<int> G[100001];
int color[100001];

void dfs( int v, int c ){
    color[v] = c;
    for( auto nv : G[v] ){
        if( color[nv] ) continue;
        dfs( nv, c );
    }
}

signed main(){
    int N, M; cin >> N >> M;
    REP( i, M ){
        int s, t; cin >> s >> t;
        G[s].push_back( t );
        G[t].push_back( s );
    }

    int c = 1;
    REP( i, N ){
        if( color[i] == 0 ){
            dfs( i, c );
            c++;
        }
    }

    int Q; cin >> Q;
    REP( i, Q ){
        int s, t; cin >> s >> t;
        if( color[s] == color[t] ){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <unordered_map>

using namespace std;

template<typename ISTREAM>
void readGraphListFromFriendInfo(ISTREAM& cin, vector< vector<int> >& friendGraphList) {
	size_t n;
	cin >> n;
	friendGraphList = vector< vector<int> >(n);
	size_t m;
	cin >> m;
	for (size_t i = 0; i != m; ++i) {
		size_t s,t;
		cin >> s >> t;
		friendGraphList.at(s).push_back(t);
		friendGraphList.at(t).push_back(s);
	}
	for (auto& v : friendGraphList)v.shrink_to_fit();
}

const vector<bool>& accesibleNodeSearch(const vector<vector<int>>& graphList
	, unordered_map<int, vector<bool>>& visitMatrix
	, const int s
	) 
{
	pair<unordered_map<int, vector<bool>>::iterator, bool> result = visitMatrix.emplace(s,vector<bool>());
	vector<bool>& visitList = result.first->second;
	if (result.second) {//未計算の場合のみ計算する
		const size_t n = graphList.size();
		queue<int> nodeQueue;
		visitList.resize(n,false);//resizeは必ずしもその値で埋めてくれない？
		nodeQueue.push(s);
		visitList.at(s).flip();
		while (!nodeQueue.empty()) {
			const int id = nodeQueue.front();
			const vector<int>& adjList = graphList.at(id);
			for (const auto& adjID : adjList) {
				if (visitList.at(adjID))continue;
				const vector<int>& idList = graphList.at(adjID);
				if (find(idList.begin(), idList.end(), id) == idList.end())continue;
				nodeQueue.push(adjID);
				visitList.at(adjID).flip();
			}
			nodeQueue.pop();
		}
	}
	return visitList;
}

template<typename T>
void execute(T& cin) {
	vector< vector<int> > graphList;
	readGraphListFromFriendInfo(cin, graphList);
	unordered_map<int, vector<bool>> visitMatrix;
	int q, s, t;
	cin >> q;
	for (int i = 0; i != q; ++i) {
		cin >> s >> t;
		const vector<bool>& visitList = accesibleNodeSearch(graphList, visitMatrix, s);
		cout << (visitList.at(t) ? "yes" : "no") << endl;
	}
}

int main()
{


	execute(cin);


	return 0;
}