Go: package main

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

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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;

}