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>
#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++: #include <bits/stdc++.h>
using namespace std;

constexpr int MAX = 100010;

struct Edge{
    int v;
    Edge* next;
};

int N, M;
Edge* G[MAX];
int component[MAX];

void push( int u, int v ){
    Edge* e = new Edge;
    e->v = v;
    e->next = nullptr;

    Edge* i;
    for( i = G[u]; i->next != nullptr; i = i->next ){}
    i->next = e;
}

void dfs( int s, int k ){
    component[s] = k;
    for( Edge* e = G[s]->next; e != nullptr; e = e->next ){
        if( !component[e->v] ){
            dfs( e->v, k );
        }
    }
}

int main(){
    for( int i = 0; i < MAX; i++ ){
        Edge* e = new Edge;
        e->v = -1;
        e->next = nullptr;       
        G[i] = e;
    }
    
    cin >> N >> M;
    for( int i = 0; i < M; i++ ){
        int u, v; cin >> u >> v;
        push( u, v );
        push( v, u );
    }

    int k = 1;
    for( int i = 0; i < N; i++ ){
        if( !component[i] ){
            dfs( i, k );
            k++;
        }
    }

    int q; cin >> q;
    for( int i = 0; i < q; i++ ){
        int u, v; cin >> u >> v;
        if( component[u] == component[v] ){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>

void dfs(int node, std::vector<std::vector<int>>& v, std::vector<int>& colors, int color) {
    colors[node] = color;
    for (int i=0; i<v[node].size(); i++) {
        if (node != v[node][i] and colors[v[node][i]] == 0) {
            dfs(v[node][i], v, colors, color);
        }
    }
} 

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, m, q, s, t, color=1;
    std::cin >> n >> m;
    std::vector<std::vector<int>> v(n);
    std::vector<int> colors(n);
    for (int i=0; i<m; i++) {
        std::cin >> s >> t;
        v[s].push_back(t);
        v[t].push_back(s);
    }
    for (int i=0; i<n; i++) {
        if (colors[i] == 0) {
            dfs(i, v, colors, color++);
        }
    }
    std::cin >> q;
    for (int i=0; i<q; i++) {
        std::cin >> s >> t;
        if (colors[s] == colors[t] and colors[s] != 0) {
            std::cout << "yes\n";
        } else {
            std::cout << "no\n";
        }
    }

    return 0;
}

C++: /*
* @Author: Samson
* @Date:   2018-05-07 21:12:41
* @Last Modified by:   Samson
* @Last Modified time: 2018-05-07 21:44:27
*/
//   @URL : 
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<stack>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

int n,m,q,color[MAXN];
vector<int> v[MAXN];

void dfs(int x,int id)
{
	stack<int> s;
	int t;
	s.push(x);
	color[x] = id;
	while(s.size())
	{
		t = s.top();
		s.pop();
		for(int i = 0; i < v[t].size(); ++i)
		{
			int val = v[t][i];
			if(color[val] == -1)
			{
				color[val] = id;
				s.push(val);
			}
		}
	}
}

void fun()
{
	int id = 0;
	memset(color,-1,sizeof color);
	for(int i = 0; i < n; ++i)
		if(color[i] == -1)
			dfs(i,++id);
}

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n>>m;
	int s,e;
	for(int i = 0; i < m; ++i)
	{
		cin>>s>>e;
		v[s].push_back(e);
		v[e].push_back(s);
	}
	fun();
	cin>>q;
	while(q--)
	{
		cin>>s>>e;
		if(color[s] == color[e] && color[s]!=-1)
			cout<<"yes\n";
		else
			cout<<"no\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;
    d[a]=h;
    d[b]=h;
d[d[a]]=h;
d[d[b]]=h;

}

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;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

#define N 100000
#define NIL -1

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

queue<int> Q;

void bfs(int x, int c){
    if(color[x] != NIL) return;
    Q.push(x);
    color[x] = c;

    while(!Q.empty()){
        int u = Q.front(); Q.pop();
        for(int i = 0; i < G[u].size(); i++){
                int v = G[u][i];
                if(color[v] == NIL)
                Q.push(v);
                color[v] = c;
        }
    }
}

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


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

    rep(i,m){
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    assignColor();

    int q;
    cin >> q;
    rep(i,q){
        int x, y;
        cin >> x >> y;
        if(color[x] == color[y]) cout << "yes" << endl;
        else cout << "no" << endl;
    }

    return 0;

}