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<iostream>
#include<iomanip>
#include<queue>
#include<string>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cassert>
#include<ctime>
#include<algorithm>
#include<utility>
#include<map>
#include<set>
#include<vector>
#include<functional>

using namespace std;

#ifdef DEBUG
#define dprintf      printf
#define dout(x)      cout<<#x" = "<<(x)<<endl
#define darray(x,n)    {int i;rep(i,n)cout<<#x"["<<i<<"] = "<<*((x)+i)<<endl;}
#define dloop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define drep(i,n)    for((i)=0;(i)<(n);(i)++)
#define dstop()      scanf("%*c")
#else
#define dprintf      (1)?0:printf
#define dout(x)
#define darray(x,n)
#define dloop(i,s,g) if(1){}else
#define drep(i,n)    if(1){}else
#define dstop()      if(1){}else
#endif

#define F    first
#define S    second
#define pb   push_back
#define mp   make_pair

#define loop(i,s,g) for((i)=(s);(i)<(g);(i)++)
#define rep(i,n)    for((i)=0;(i)<(n);(i)++)
#define all(x)      (x.begin(),x.end())
#define in(T,...) T __VA_ARGS__; impl(__VA_ARGS__);
#define array(T,id,n) T id[n]; rep(i,n)cin>>id[i];

//#define int long long;
#ifndef int
#define INF  (0x7fffffff)
#else
#define LINF (0x7fffffffffffffff)
#endif

typedef  long long           ll;
typedef  unsigned            ui;
typedef  unsigned long long  ull;
typedef  pair<int,int>       i_i;
typedef  pair<ll,int>        ll_i;
typedef  pair<ll,ll>         ll_ll;
typedef  pair<double,int>    d_i;
typedef  pair<double,double> d_d;
/*
void impl(){};
template <typename T,typename... TS >
void impl(T &head,TS &... tail)
{
  cin>>head;
  impl(tail ...);
}
*/
int e[112345][112345]={};
int sumi[112345]={};
int n,m;

int dfs(int v,int g)
{
  int i;
  int ret=0;
  dout(v);
  if(v==g)
    return 1;
  rep(i,n)
    if(e[v][i]==1 && sumi[i]==0)
      {
	sumi[i]=1;
	ret=dfs(i,g);
	if(ret==1)
	  break;
      }
  return ret;
}
signed main(void)
{
  int i;
  int a,b;
  int q;
  int s,t;
  cin>>n>>m;
  rep(i,m)
    {
      cin>>a>>b;
      e[a][b]=1;
      e[b][a]=1;
    }
  cin>>q;
  rep(i,q)
    {
      cin>>s>>t;
      if(dfs(s,t))
	cout<<"yes"<<endl;
      else
	cout<<"no"<<endl;
    }    
  return 0;
}

C++: //
//  main.cpp
//  connected_components
//
//  Created by ???????????? on 2017/10/22.
//  Copyright ?? 2017 ????????????. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <vector>

using namespace std;

#define n_max 100000
//#define vector_max 100000
#define WHITE 0
#define GRAY 1
#define BLACK 2

bool found;
int n, target, t = 0;
int color[n_max], d[n_max], f[n_max];
vector<int> G[n_max];

void encode_graph_list(){
    int i, id, node, f;
    
    scanf("%d", &n);
    scanf("%d", &node);
    
    for (i = 0; i < node; i++){
        scanf("%d", &id);
        scanf("%d", &f);
        G[id].push_back(f);
    }
    
}

void dfs(int u){
//    found = false;
//    if(target == u) {
//        found = true;
//        return;
//    }
    color[u] = GRAY;
    d[u] = ++t;
    for (int v = 0; v < G[u].size(); v++){
        int t = G[u][v];
        if(color[t] == WHITE){
            dfs(t);
        }
    }
    color[u] = BLACK;
    f[u] = ++t;
}

void dfs_init(int start){
    for(int i = 0; i < n ; i++){
        color[i] = WHITE;
    }
    dfs(start);
}



int main(int argc, const char * argv[]) {
    int qs, i, id;
    encode_graph_list();
    scanf("%d", &qs);
    for (i = 0; i < qs; i++){
        scanf("%d %d", &id, &target);
        dfs_init(id);
        if(color[target] == color[id]) cout << "yes" <<endl;
        else cout << "no"<<endl;
    }
//    std::cout << "Hello, World!\n";
    return 0;
}

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

#define ll                  long long
#define scl(n)              scanf("%lld", &n)
#define fr(i,n)             for (ll i=0;i<n;i++)
#define fr1(i,n)            for(ll i=1;i<=n;i++)
#define pfl(x)              printf("%lld\n",x)
#define endl 	            "\n"
#define pb                  push_back
#define l(s)                s.size()
#define asort(a)            sort(a,a+n)
#define dsort(a)            sort(a,a+n,greater<int>())
#define vasort(v)           sort(v.begin(), v.end());
#define vdsort(v)           sort(v.begin(), v.end(),greater<int>());
#define pn                  printf("\n")
#define md                  10000007

ll n;
vector<ll>v[1000005];
ll cnt=1;
bool vis[1000005];
ll clr[1000005];
queue<ll>q;
void bfs(ll s, ll d)
{
    q.push(s);
    clr[s]=d;
    while(!q.empty())
    {
         ll p=q.front(); q.pop();
         for(ll i=0;i<v[p].size();i++)
         {
             ll u=v[p][i];
             if(clr[u]==-1)
             {
                 q.push(u);
                 clr[u]=d;
             }
         }
    }
}

void colour( )
{
    fr(i,1000000)clr[i]=-1;

    fr(i,n)if(clr[i]==-1)bfs(i, cnt++);
}
int main()
{
    ll m,t,b,c,d,i,j,k,x,y,z,l,q,r;

      ll cnt=0,ans=0;
      scl(n);scl(m);

      fr(i,m)
      {
          cin>>x>>y;
          v[x].pb(y);
          v[y].pb(x);
      }
      colour();

      cin>>q;
      while(q--)
      {
          cin>>x>>y;
          if(clr[x]==clr[y])cout<<"yes"<<endl;
          else cout<<"no"<<endl;
      }
return 0;
}