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 <iostream>
#include <vector>
#define eol '\n'
using namespace std;

vector< vector<int> > list;
vector<int> group;

void bfs(int v, int g) {
  if (group[v] != 0) {
    return;
  }
  group[v] = g;
  for (int i = 0; i < list[v].size(); i++) {
    bfs(list[v][i], g);
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n,m,s,t,q;
  cin >> n >> m;

  list = vector< vector<int> >(n, vector<int>());
  group = vector<int>(n,0);
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    list[s].push_back(t);
    list[t].push_back(s);
  }

  int g = 1;
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      bfs(i, g);
      g++;
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (group[s] == group[t]) {
      cout << "yes" << eol;
    } else {
      cout << "no" << eol;
    }
  }
  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(j!=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 <iostream>
#include <vector>
using namespace std;

const int N = 2e5;
int used[N];
int head[N];
vector<int> fr[N];
int n, m, a, b;

void dfs(int rt, int now){
	used[now] = 1;
	head[now] = rt;
	for(int i=0; i<fr[now].size(); i++){		
		if(used[fr[now][i]] == 0){
			dfs(rt, fr[now][i]);
		}		
	}
}

void Sovle(){
	for(int i=0; i<n; i++){
		if(used[i]==0)dfs(i,i);
	}
}

void Output(){
	cin>>m;
	for(int i=0; i<m; i++){
		cin>>a>>b;
		if(head[a]==head[b])cout<<"yes\n";
		else cout<<"no\n";
	}
}

void Input(){
	cin>>n>>m;
	for(int i=0; i<m; i++){
 		cin>>a>>b;
 		fr[a].push_back(b);
 		fr[b].push_back(a);
 	}	
}

int main(){
	Input();
	Sovle();
	Output();
	return 0;
}

C++: #include<iostream>
#include<cstdlib>
using namespace std;
int *graph[100000],num,stack[200000],top,used[100000],gsize[100000];
void dfs(int x) {
  stack[top++] = x;
  used[x] = num;
  while (top != 0) {
    int y = stack[--top];
    for (int i = 0; i < gsize[y]; ++i) {
      int ver = graph[y][i];
      if (used[ver] == 0) {
	used[ver] = num;
	stack[top++] = ver;
      }
    }
  }
}
int main() {
  int n, m, q; cin >> n >> m;
  int ver1[100000], ver2[100000], size[100000] = {};
  for (int i = 0; i < m; ++i) {
    cin >> ver1[i] >> ver2[i];
    ++size[ver1[i]]; ++size[ver2[i]];
  }
  for (int i = 0; i < n; ++i) graph[i] = (int *)malloc(sizeof(int)*size[i]);
  for (int i = 0; i < m; ++i) {
    graph[ver1[i]][gsize[ver1[i]]++] = ver2[i];
    graph[ver2[i]][gsize[ver2[i]]++] = ver1[i];
  }
  for(int i=0;i<n;++i)
    if (used[i] == 0) {
      ++num; dfs(i);
    }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t; cin >> s >> t;
    cout << (used[s] == used[t] ? "yes" : "no") << endl;
  }
  for (int i = 0; i < n; ++i) free(graph[i]);
  return 0;
}

C++: #include<iostream>
#include<stack>
#include<vector>

using namespace std;

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

void dfs (int r, int c) {

    int i, u, v;

    stack<int> S;
    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] == -1) {
                color[v] = c;
                S.push(v);
            }
        }
    }

}

void assignColor () {

    int id = 1, i, u;

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

}

int main () { 

    int s, t, m, q, i;

    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]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;

}