Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

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

using namespace std;

int main() {

	int n, m;
	cin >> n >> m;
	vector < vector<int>> G(n + 1);

	//グラフ隣接リスト読み込み
	int a, c;
	for (int i = 1; i <= m; i++) {
		cin >> a >> c;
		G[a].push_back(c);
		G[c].push_back(a);
	}

	//幅優先探索処理
	vector<bool> seen(n + 1, false);	//発見したらtrue
	deque<int> todo;					//行く所のスタック

	int s = 1;							//今いる場所

	bool brkflg;
	vector<long> color(n + 1);
	int colornum = 0;
	for (int j = 0; j < n; j++) {		//全ての頂点を調べる

		if (color[j] == 0) {
			colornum++;
			color[j] = colornum;
			brkflg = false;
			todo.clear();

			//for (int k = 0; k <= n; k++) {
			//	seen[k] = false;//一旦全クリア
			//}

			if (seen[j] == false) {			//訪れてなかったら処理すすめる
				seen[j] = true;
				todo.push_back(j);

				while (todo.empty() != true) {				//キューが空になるまで処理	
					s = todo.front();						//処理番号を指定
					int tugiflg = false;					//次があったらtrueにするフラグ
					for (int i = 0; i < G[s].size(); i++) {

						if (seen[G[s][i]] != true) {
							seen[G[s][i]] = true;
							color[G[s][i]] = colornum;
							todo.push_back(G[s][i]);
							tugiflg = true;
						}
					}
					todo.pop_front(); //次を全部探したので削除する
				}
			}
		}
	}

	int in1, in2;
	cin >> m;
	for (int i = 0; i <m; i++) {
		cin >> in1 >> in2;
		if (color[in1] == color[in2]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}

	return 0;

}

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 ] )
		{
			printf( "yes\n" );
		}
		else
		{
			printf( "no\n" );
		}
	}

	delete[] user_info;
	return 0;
}

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

bool DFS(const vector<vector<int> >& G, int a, int b) {
    stack<pair<int, int> > S;
    vector<bool> is_visited(G.size(), false);

    int idx = 0;
    do {
        if (G[a][idx] == b) return true;
        if (!is_visited[G[a][idx]]) {
            is_visited[G[a][idx]] = true;
            S.push(make_pair(a, idx));
            a   = G[a][idx];
            idx = 0;
            continue;
        }
        if (idx >= G[a].size()) {
            a   = S.top().first;
            idx = S.top().second;
            S.pop();
        }
        ++idx;
    } while (!S.empty());
    return false;
}


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;
        if (DFS(G, a, b)) {
            cout << "yes" << endl;
        } else {
            cout << "no"  << endl;
        }
    }
    return 0;
}