Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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;
}