Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

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

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


class UNF {
    public:
        std::vector<int> p, rank;
        UNF(int s) {
            p.resize(s, 0);
            rank.resize(s, 0);
            for (int i = 0; i < s; i++) {
                makeset(i);
            }
        }
        void makeset(int x) {
            p[x] = x;
            rank[x] = 0;
        }
        int findset(int x) {
            if (x != p[x]) {
                p[x] = findset(p[x]);
            }
            return p[x];
        }
        
        void link(int x, int y) {
            if (rank[x] > rank[y]) {
                p[y] = x;
            }
            else {
                p[x] = y;
            }
            if (rank[y] == rank[x]) {
                rank[y]++;
            }
        }
        
        void unite(int x, int y) {
            link(findset(x), findset(y));
        }

};

int main() {
    int n, m, q;
    std::cin >> n >> m;
    UNF unf = UNF(n);
    for (int i = 0; i < n; i++) {
        unf.makeset(i);
    }
    int a, b;
    for (int j = 0; j < m; j++) {
        std::cin >> a >> b;
        unf.unite(a, b);
    }
    std::cin >> q;
    for (int p = 0; p < q; p++) {
        std::cin >> a >> b;
        if (unf.findset(a) == unf.findset(b)) {
            std::cout << "yes" << std::endl;
        }
        else {
            std::cout << "no" << std::endl;
        }
    }
}

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

static const int MAX = 100000;
static const int NIL = -1;


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


void dfs(int r, int c){
    stack<int> S;
    S.push(r);
    color[r] = 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 assignColor(){
    int id = 1;
    for(int i = 0; i < n; i++)
    color[i] = NIL;
    for(int u = 0; u < n; u++){
        if(color[u] == NIL)  dfs(u, 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);
    
}
    
    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++: import sys
sys.setrecursionlimit(50000)
def dfs(s):
    global t
    if s == t:
        return True
    DP[s]=1
    for i in F[s]:
        if not DP[i]:
            if dfs(i):
                return True
    return False
    
n,m = [int(i) for i in input().split()]
F = [{} for i in range(n)]
for _ in range(m):
    i,j = [int(i) for i in input().split()]
    F[i][j] = 1
    F[j][i] = 1
q = int(input())
for i in range(q):
    DP = [0]*n
    s,t = [int(i) for i in input().split()]
    if dfs(s):
        print("yes")
    else:
        print("no")