Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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")