Go: package main

import (
	"bufio"
	"container/list"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

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

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

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

C++: #include<iostream>
#include<vector>
#inlcude<stack>
using namespace std;
#define MAX 100000
#define NIL -1

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


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



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

int main() {

  int  m, i, j, v, s, t, q;

  cin >> n >> m;

  for (i=0; i<m; i++) {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (i=0; i<q; i++) {
    cin >> s >> t;
    if (color[s]== color[t]) {
      printf("yes\n");
    } else {
      printf("no\n");
    }
  }

  return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long unsigned int ll;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define NIL -1
#define MAX 100000

ll conv(string num) { stringstream ss; ss << num << flush; ll n; ss>>n; return n; }

string conv(ll n) { stringstream ss; ss << n << flush; return ss.str(); }

vector<string> split(string s, string delim) {
    vector<string> res;
    int pos = 0;
    while(true) {
        int found = s.find(delim, pos);
        if(found >= 0) {
            res.push_back(s.substr(pos, found - pos));
        }
        else {
            res.push_back(s.substr(pos));
            break;
        }
        pos = found + delim.size();
    }
    return res;
}
int ALPH_SIZE = 26;
char ALPH_L[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char ALPH_U[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

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

void dfs(int r, int c) {
    stack<int> s;
    s.push(r);

    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<MAX; i++) {
        color[i] = NIL;
    }

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



int main() {
    int v, w, m, n, s, t;
    cin >> n >> m;
    for(int i=0; i<m; i++) {
        cin >> v >> w;
        G[v].push_back(w);
        G[w].push_back(v);
    }

    assignColor();

    int q; 

    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 <algorithm>
#include <iostream>
#include <string>
#include <vector>

class TUser
{
	std::vector<TUser*> FAccessible;
	bool FVisited;

public:
	TUser();
	void AddFriend(TUser* Friend);
	bool IsAccesible(TUser* Target);
};

TUser::TUser()
:FVisited(false)
{
}

void TUser::AddFriend(TUser* Friend)
{
	if(std::find(FAccessible.begin(), FAccessible.end(), Friend) != FAccessible.end()) return;
	FAccessible.push_back(Friend);
}

bool TUser::IsAccesible(TUser* Target)
{
	if(FVisited) return false;
	FVisited = true;
	if(std::find(FAccessible.begin(), FAccessible.end(), Target) != FAccessible.end()) return true;
	for(unsigned int i = 0; i < FAccessible.size(); ++i){
		if(FAccessible[i]->IsAccesible(Target)) return true;
	}
	return false;
}

class TSns
{
	std::vector<TUser> FUsers;
public:
	TSns(int UserCount);
	
	void AddLink(int UserA, int UserB);
	std::string IsAccesible(int UserA, int UserB);
};

TSns::TSns(int UserCount)
: FUsers(UserCount)
{
}

void TSns::AddLink(int UserA, int UserB)
{
	FUsers[UserA].AddFriend(&FUsers[UserB]);
	FUsers[UserB].AddFriend(&FUsers[UserA]);
}

std::string TSns::IsAccesible(int UserA, int UserB)
{
	if(FUsers[UserA].IsAccesible(&FUsers[UserB])) return "yes";
	return "no";
}

int main()
{
	int UserCount;
	std::cin >> UserCount;
	TSns Sns(UserCount);
	
	int LinkCount;
	std::cin >> LinkCount;
	for(int i = 0; i < LinkCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		Sns.AddLink(UserA, UserB);
	}
	
	int QuestionCount;
	std::cin >> QuestionCount;
	for(int i = 0; i < QuestionCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		std::cout << Sns.IsAccesible(UserA, UserB) << std::endl;
	}
	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];
}
if(d[j]==j){
	while(d[h]!=h){
		h=d[h];
	}
d[j]=h;
d[a]=h;
d[b]=h;
}
else{
	while(d[j]!=j){
		j=d[j];
	}
d[h]=j;
d[a]=j;
d[b]=j;
}

}
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++: 
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e5+5;
const int NIL=-1;

int n,color[maxn];
vector<int>g[maxn];

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;
}
/*
10 9
0 1
0 2
3 4
5 7
5 6
6 7
6 8
7 8
8 9
3
0 1
5 9
1 3
*/