Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int maxx=100100;
vector<int> A[maxx];
int color[maxx];
int c=1;

void dfs(int u){
	if(color[u]!=-1) return ;
	color[u]=c;
	int si=A[u].size();
	for(int i=0;i<si;i++){
		if(color[A[u][i]]==-1){
			dfs(A[u][i]);
		}
	}
	return ;
}

int main(){
	int n,q,a,b;// why i should put the dim n in main but not global
	cin>>n>>q;
	for(int i=0;i<=maxx;i++) color[i]=-1;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		A[a].push_back(b);
		A[b].push_back(a);
	}
	for(int i=0;i<n;i++){
		dfs(i);
		c++;

	}
//	for(int i=0;i<n;i++){
//		cout<<i<<" "<<color[i]<<endl;
//	}
	
	cin>>q;
	for(int i=1;i<=q;i++){
		cin>>a>>b;
		if(color[a]==color[b]) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
const ll INF = 1 << 21;
const ll MOD = 1e9 + 7;

static const int N = 1e5;
static const int NIL = -1;

int n, m, q;
vector<int> G[N];
int color[N];

void dfs(int r, int c)
{
    stack<int> S;
    S.push(r);
    color[r] = c;
    while(!S.empty()) {
        int u = S.top();
        S.pop();
        rep(i, G[u].size()) {
            int v = G[u][i];
            if (color[v] == NIL) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assigncolor()
{
    int id = 1; // 同じidがcolorに格納されているときは同じ連結成分にある
    // 初期化
    rep(i, n) {
        color[i] = NIL;
    }
    rep(u, n) {
        if (color[u] == NIL) { // 初期化されていた場合
            dfs(u, id++);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    rep(i, m)
    {
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assigncolor();

    cin >> q;
    rep(i, q)
    {
        int s, t;
        cin >> s >> t;
        if (color[s] == color[t]) {
            puts("yes");
        } else {
            puts("no");
        }
    }
    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);
    dfs_init(0);
    for (i = 0; i < qs; i++){
        scanf("%d %d", &id, &target);
        if(color[target] == color[id]) cout << "yes" <<endl;
        else cout << "no"<<endl;
    }
//    std::cout << "Hello, World!\n";
    return 0;
}

C++: #include <iostream>
using namespace std;
 
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
d[j]=h;
while(a!=h){
 
    d[a]=h;
    d[b]=h;
a=d[a];
b=d[b];
}
}

cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;

#define REP(i,n) for(int i=0;(i)<(n);(i)++)

vector<int> adjacencyList[100005];//??£??\?????????

int n;
int color[100005];//????????????????????°?????????????±???????????????????


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 < adjacencyList[u].size();i++) {
			int v = adjacencyList[u][i];
			if (color[v] == -1) {
				color[v] = c;
				S.push(v);
			}
		}
	}
}

void assignColor() {
	REP(i, n) color[i] = -1; //?????????

	//?????????
	int next_color = 1;
	REP(i, n) {
		if (color[i] == -1) {
			dfs(i, next_color++);
		}
	}
}


int main() {
	int m;
	cin >> n >> m;

	int s, t;
	REP(i, m) {
		cin >> s >> t;
		adjacencyList[s].push_back(t);
		adjacencyList[t].push_back(s);
	}

	assignColor();//?????????

	int q;
	cin >> q;
	REP(i, q) {
		cin >> s >> t;//s??????t?????????????????¨???????????¨???

		(color[s] == color[t]) ? cout << "yes" : cout << "no";
		cout << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
#include<utility>
#include<vector>

using namespace std;



int main() {
	int cnt_people, cnt_relation;
	queue<int> q;
	int cnt_equiv = 0;
	cin >> cnt_people >> cnt_relation;

	vector<int> *adjacent=new vector<int>[cnt_people];
	int* equiv = new int[cnt_people];
	for (int i = 0;i < cnt_people;i++) {
		equiv[i] = -1;
	}
	int tmp1, tmp2;
	for (int i = 0;i < cnt_relation;i++) {
		cin >> tmp1>>tmp2;
		adjacent[tmp1].push_back(tmp2);
		adjacent[tmp2].push_back(tmp1);
	}
	for (int i = 0;i < cnt_people;i++) {
		if (equiv[i] == -1) {
			cnt_equiv++;
			equiv[i] = cnt_equiv;
			q.push(i);
			while (!q.empty()) {
				tmp1 = q.front();
				q.pop();
				for (int j = 0;j < adjacent[tmp1].size();j++) {
					if (equiv[adjacent[tmp1][j]] == -1) {
						equiv[adjacent[tmp1][j]] = cnt_equiv;
						q.push(adjacent[tmp1][j]);
					}
				}
			}
		}
	}
	int cnt_question;
	cin >> cnt_question;
	for (int i = 0;i < cnt_question;i++) {
		cin >> tmp1 >> tmp2;
		if (equiv[tmp1] == equiv[tmp2]) {
			cout << "yes\n";
		}
		else {
			cout << "no\n";
		}
	}
}