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

C++: #include <bits/stdc++.h>
using namespace std;
static const int MAX= 100001;
int N,color[MAX],id=1;
vector<int> G[MAX];

void dfs(int n,int c)
{
	stack<int> s;
	s.push(n);
	color[n]=c;
	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]!=-1) continue;
			color[v]=c;
			s.push(v);
		}
	}
}

void assignColor()
{
	for(int i=0;i<N;i++) color[i]=-1;
	for(int i=0;i<N;i++)
	{
		if(color[i]==-1) dfs(i,id++);
	}
}
int main()
{
	int m,s,t,q;
	cin>>N>>m;
	for(int i=0;i<m;i++)
	{
		cin>>s>>t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	assignColor();
	
	cin>>q;
	for(int i=0;i<q;i++)
	{
		cin>>s>>t;
		if(color[s]==color[t]) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
const int MAXN = 100005;
const int NIL = -1;

int n;
vector<int>G[MAXN];
int color[MAXN];

void dfs(int s, int c)
{
	stack<int> S;
	S.push(s);
	color[s] = c;
	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] = c;
				S.push(v);
			}
		}	
	}
}

void AssighColor()
{
	int id = 1;
	for(int i = 0; i < n; i++)
		color[i] = NIL;
	for(int i = 0; i < n; i++)
	{
		if(color[i] == NIL)
			dfs(i, id++);
	}
}

int main()
{
	int s, t, m, q;
	cin >> n >> m;
	for(int i = 0; i < m; i++)
	{
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	
	AssighColor();
	
	cin >> q;
	for(int i = 0; i < q; i++)
	{
		cin >> s >> t;
		if(color[s] == color[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}