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++: #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;

}