Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include<bits/stdc++.h>
using namespace std;
const int MAX = 100000;
const int NIL = -1;
int n;
vector<int> G[MAX];
int color[MAX];
void dfs(int r,int c) {
    if(color[r] == NIL) color[r] = c;
    for(int i = 0;i < G[r].size();i++) {
        int v = G[r][i];
        if(color[v] == NIL) dfs(v,c);
    }
}
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;
    scanf("%d%d",&n,&m);
    for(int i = 0;i < m;++i) {
        scanf("%d%d",&s,&t);
        G[s].push_back(t);
        G[t].push_back(s);
    }
    assignColor();
    scanf("%d",&q);
    for(int i = 0;i < q;++i) {
        scanf("%d%d",&s,&t);
        if(color[s] == color[t]) printf("yes\n");
        else printf("no\n");
    }
    return 0;
}

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 >> s >> 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 <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <map>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

const int N = 100000;
int n, m, q;
vector<int> M[N];
bool V[N];

int bfs(int u, int v) {
  int s;
  queue<int> q;
  REP(i, n) V[i] = false;

  s = u;
  q.push(s);
  V[s] = true;

  while (!q.empty() && !V[v]) {
    s = q.front(); q.pop();
    REP(i, M[s].size()) {
      int t = M[s][i];
      if (!V[t]) {
        q.push(t);
        V[t] = true;
      }
    }
  }

  return V[v];
}

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

  cin >> n >> m;
  REP(i, m) {
    int u, v;
    cin >> u >> v;
    M[u].push_back(v);
    M[v].push_back(u);
  }

  cin >> q;
  REP(i, q) {
    int u, v;
    string answer;
    cin >> u >> v;
    answer = bfs(u, v) ? "yes" : "no";
    cout << answer << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <queue>
static const int MAX=100000;
using namespace std;
vector<int> follow[MAX];
int color[MAX];  //-1 represent No Visitedvoid 
bfs(int i,int c){    queue<int> Q;    Q.push(i);color[i]=c;    
while(!Q.empty())  
  {        int p=Q.front();Q.pop();        for(int j=0;j<follow[p].size();j++)        {            if(color[follow[p][j]]!=-1) continue;            //else            Q.push(follow[p][j]); color[follow[p][j]]=color[p];        }    }}
int main(){   int n,m;   cin>>n>>m;   for(int i=0;i<n;i++) color[i]=-1;   for(int i=0;i<m;i++)   {       int a,b;       cin>>a>>b;       follow[a].push_back(b);       follow[b].push_back(a);   }   int c=0;   for(int i=0;i<n;i++)   {       if(color[i]==-1)       {           bfs(i, c);           c++;       }   }   int order_num;cin>>order_num;   for(int i=0;i< order_num;i++)   {       int a,b;       cin>>a>>b;       if(color[a]==color[b]) cout<<"yes"<<endl;       else cout<<"no"<<endl;   }}

C++: #include <stdio.h>
#include <vector>
#include <stack>
#define MAX 100010
#define INFTY 10000000
#define NIL -1

using namespace std;

int N;
int M;
vector<int> V[MAX];
int color[MAX];

int dfs(int s, int t)
{
	stack<int> S;
	S.push(s);
	color[s] = t;
	
	while (!S.empty()) {
		int u = S.top(); S.pop();
		for (int i=0; i<V[u].size(); i++) {
			int v = V[u][i];
			if (color[v] == NIL) {
				color[v] = t;
				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 i, n, u, v;	
	scanf("%d %d", &N, &M);	
	for (i = 0; i < M; i++) {
		scanf("%d %d", &n, &v);
		V[n].push_back(v);
		V[v].push_back(n);
	}
	
	assignColor();
	
	int Q;
	scanf("%d", &Q);
	for (i = 0; i < Q; i++) {
		scanf("%d %d", &u, &v);
		/*
		if (dfs(u, v) == 1) {
			puts("yes");
		}
		else {
			puts("no");
		}*/
		if (color[u] == color[v]) {
			puts("yes");			
		}
		else {
			puts("no");
		}
	}

	return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

class UnionFind
{
    vector<int> par;

public:
    UnionFind(int n) : par(n, -1) {}

    int root(int x)
    {
        if (par[x] < 0)
            return x;
        else
            return par[x] = root(par[x]);
    }

    bool merge(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x == y)
            return false;
        if (par[x] > par[y])
            swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }

    int size(int x) { return -par[root(x)]; }
};

void solve()
{
    int n, m, q;
    cin >> n >> m;
    UnionFind uf(n);
    while (m--)
    {
        int s, t;
        cin >> s >> t;
        uf.merge(s, t);
    }
    cin >> q;
    while (q--)
    {
        int s, t;
        cin >> s >> t;
        if (uf.root(s) == uf.root(t))
        {
            cout << "yes\n";
        }
        else
        {
            cout << "no\n";
        }
    }
}

int main()
{
    solve();
    return 0;
}