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<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<sstream>
#include<cmath>
#include<numeric>
#include<map>
#include<stack>
#include<queue>
using namespace std;
int inf = 1000000000;

class UF{
public:
  vector<int> u;
  vector<int> r;

  void init(int n){
    u.resize(n, 0);
    r.resize(n, 0);
    for(int i=0; i<n; i++) u[i] = i;
  }
  int root(int x){
    // if( u[x] == x ) return x;
    // else return root(u[x]);
    return u[x] == x ? x : u[x] = root(u[x]);
  }
  bool same(int x, int y){
    return root(x) == root(y);
  }
  void unite(int x, int y){
    x = root(x);
    y = root(y);
    if( x == y ) return;

    if( r[x] < r[y] ){
      u[x] = y;
    }else{
      u[y] = x;
      if( r[x] == r[y] ) r[x]++;
    }
  }
};

int main(void) {

  int n, m;
  cin >> n >> m;
  UF uf;
  uf.init(n);
  int s, t;
  for(int i=0; i<m; i++){
    scanf("%d %d", &s, &t);
    uf.unite(s, t);
  }
  int q;
  cin >> q;
  for(int i=0; i<q; i++){
    scanf("%d %d", &s, &t);
    cout << ( uf.same(s, t) ? "yes" : "no" ) << endl;
  }



  return 0;
}

// EOF

C++: #include <iostream>
#include <vector>
#include <unordered_set>
#include <algorithm>
//#include "../cxx-prettyprint/prettyprint.hpp"

std::vector<std::vector<int>> Adj;
std::unordered_set<int> nodes_visited;

bool is_reachable(int s, int t) {
    if (std::find(Adj[s].begin(), Adj[s].end(), t) != Adj[s].end()) {
        return true;
    } else if (nodes_visited.find(s) != nodes_visited.end()) {
        return false;
    } else {
        nodes_visited.insert(s);
        bool value = std::any_of(Adj[s].begin(), Adj[s].end(),
                                 [&](int next){return is_reachable(next, t);});
        return value;
    }
}

int main() {
    int n, m;
    std::cin >> n >> m;

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

    int q;
    std::cin >> q;
    for (int i = 0; i < q; i ++) {
        int s, t;
        std::cin >> s >> t;
        std::cout << (is_reachable(s, t) ? "yes" : "no") << std::endl;
        nodes_visited.clear();
    }
}

C++: #include<iostream>
#include<vector>
#include<stack>
#define MAX 100000
#define NIL -1
using namespace std;

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 assign()
{
  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>>n>>m;

  for(int i=0;i<m;i++)
    {
      cin>>s>>t;
      G[s].push_back(t);
      G[t].push_back(s);
    }

  assign();

  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<stdio.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
bool dfs2(int s,int t){
    if(s==t)return true;
    visited[s]=true;
    for(int i=0;i<g[s].size();i++){
        if(!visited[g[s][i]]){
            if(dfs2(g[s][i],t)){
                return true;
            }
        }
    }
    return false;
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
        g[t].push_back(s);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    visited.resize(n);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        for(int j=0;j<n;j++)visited[j]=false;
        string s1=(dfs2(s,t)?"yes":"no");
        cout<<s1<<endl;
    }
    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() == false) {
		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){
			if (color[u] == NIL) {
				dfs(u, id++);
			}
		}
	}
}

int main()
{
	int s, t, m, q;

	cin >> n >> 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;
}