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 <stdio.h>
#include <vector>
#include <queue>
#define NUM 100000
#define NIL -1
using namespace std;

int node_num;
vector<int> v[NUM + 1];
queue<int> Q;
int color[NUM + 1];

bool is_connected(int a, int b){
    if(color[a] == color[b]){
        return true;
    }else{
        return false;
    }
}

void add_next(int n){
    for(int i = 0; i < v[n].size(); i++){
        if(color[v[n][i]] != NIL)
            continue;
        Q.push(v[n][i]);
        color[v[n][i]] = color[n];
    }
}

void bfs(){
    for(int i = 1; i <= node_num; i++){
        if(color[i] != NIL)
            continue;
        color[i] = i;
        Q.push(i);
        add_next(i);

        while(Q.size()){
            int top = Q.front();
            Q.pop();
            add_next(top);
        }
    }
    return;
}

void init(){
    for(int i = 0; i <= NUM; i++){
        color[i] = NIL;
    }
}

int main(void){
    init();

    int m;
    scanf("%d", &node_num);
    scanf("%d", &m);

    for(int i = 0; i < m; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        v[a].push_back(b);
        v[b].push_back(a);
    }

    bfs();
     
    int q;
    scanf("%d", &q);
    for(int i = 0; i < q; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        if(is_connected(a, b)){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }
}

C++: #include <iostream>
#include <vector>
using namespace std;

bool search(vector<int> member,int x){
  for(int i=0; i<member.size(); i++){
    if(member[i] == x)
      return true;
  }
    return false;
}

int main(){
  int n,m,join[10000],s,t;
  vector<int> group[10000];
  cin >> n >> m;

  for(int i=0; i<n; i++)
    join[i] = i;
    
  for(int i=0; i<m; i++){
    cin >> s >> t;
    bool flag = search(group[join[t]],s);

    if(!flag){
      group[join[s]].push_back(t);
      
      for(int j=0; j<group[t].size(); j++)
	join[group[t][j]]=join[s];
	  
      join[t] = join[s];
    }
  }

  int q;
  cin >> q;
  
  bool answer[10000];
  for(int i=0; i<q; i++){
    cin >> s >> t;
    
    if(join[s] == join[t])
      answer[i]=true;
    else
      answer[i]=false;
  }

  for(int i=0; i<q; i++){
    if(answer[i])
      cout << "yes\n";
    else
      cout << "no\n";
  }
}

C++: #include <iostream>
#include <vector>
#include <cassert>
#include <queue>

const int M = 10000000;

void connected_components(
  std::vector<std::vector<int> >& adj_list,
  std::vector<int>& component_numbers,
  int root, int current_component){
  assert(component_numbers[root] == M);

  std::queue<int> target_q;
  target_q.push(root);
  component_numbers[root] = current_component;
  // dist_q.push(0;)
  while(!target_q.empty()){
    int vtx = target_q.front(); target_q.pop();
    for(int const&  u : adj_list[vtx]){
      if(component_numbers[u] == M){
        target_q.push(u);
        component_numbers[u] = current_component;
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int n, m, q;
  std::cin >> n;
  std::cin >> m;

  std::vector<std::vector<int> > adj_list(n);

  for (size_t i = 0; i < m; i++) {
    int a, b;
    std::cin >> a;
    std::cin >> b;
    adj_list[a].push_back(b);
  }

  std::vector<int> component(n, M);

  for (size_t i = 0; i < n; i++) {
    if(component[i] == M){
      connected_components(adj_list, component, i, i);
    }
  }

  std::cin >> q;
  for(size_t i =0; i < q; i++){
    int a, b;
    std::cin >> a;
    std::cin >> b;
    bool in_same_component = (component[a] == component[b]);
    std::cout << (in_same_component ? "yes" : "no") << std::endl;
  }


  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
#include<iterator>
#include<list>
using namespace std;
typedef  long long int lint;
//typedef pair<int, int> IP;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)
#define mp make_pair
#define P pair<int,int>

class UnionFindTree {
private:
	std::vector<int> par;   // 親
	std::vector<int> depth; // 木の深さ

public:
	// n要素で初期化
	UnionFindTree(int n) {
		for (int i = 0; i < n; i++) {
			par.push_back(i);
			depth.push_back(0);
		}
	}

	// 木の根を求める
	int find(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	// xとyの属する集合を併合
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)
			return;

		if (depth[x] < depth[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (depth[x] == depth[y])
				depth[x]++;
		}
	}

	// xとyが同じ集合に属するか否か
	bool same(int x, int y) { return find(x) == find(y); }
};

int main() {
	int n, m;
	cin >> n >> m;
	UnionFindTree UFT = UnionFindTree(n + 1);
	int s, t;
	rep(i, m) {
		cin >> s >> t;
		UFT.unite(s, t);
	}
	int Q;
	cin >> Q;
	rep(i, Q) {
		cin >> s >> t;
		if (UFT.same(s, t)) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
}