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 <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct Node {
  vector<int> friends;
  int depth;
};
vector<Node> nodes;


void bfs(int id) {
  queue<int> q;
  q.push(id);

  for (auto el : nodes) {
    el.depth = -1;
  }
  nodes[id].depth = 0;

  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (auto fid : nodes[u].friends) {
      if (nodes[fid].depth <= 0) {
        q.push(fid);
        nodes[fid].depth = nodes[u].depth + 1;
      }
    }
  }
}

bool isAccessible(int id){
  return (nodes[id].depth > 0);
}


int main() {
  int nrel, nuser;
  cin >> nuser;
  cin >> nrel;
  nodes.resize(nuser);
  int tmp1, tmp2;
  for (int i = 0; i < nrel; i++) {
    cin >> tmp1 >> tmp2;
    nodes[tmp1].friends.push_back(tmp2);
    nodes[tmp2].friends.push_back(tmp1);
  }

  int q, s, t;
  cin >> q;
  for (int i = 0; i< q; i++) {
    cin >> s >> t;
    bfs(s);
    cout << (isAccessible(t) ? "yes" : "no") << endl;
  }
}

C++: #include <cstdio>
#include <algorithm>
#include <unordered_set>
#include <vector>

int main(){
    int n;
    int m;
    std::scanf("%d", &n);
    std::scanf("%d", &m);
    std::vector<std::unordered_set<int> > relations;
    for(int i = 0; i < m; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        int finds = -1;
        int findt = -1;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end()){
                finds = j;
                break;
            }
        }
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(t) != relations[j].end()){
                findt = j;
                break;
            }
        }
        if(finds == -1 && findt == -1){
            std::unordered_set<int> set;
            set.insert(s);
            set.insert(t);
            relations.push_back(set);
        } else if(findt == -1){
            relations[finds].insert(t);
        } else if(finds == -1){
            relations[findt].insert(s);
        } else if(finds != findt){
            int smaller = std::min(finds, findt);
            int larger = std::max(finds, findt);
            for(auto &x: relations[larger]){
                relations[smaller].insert(x);
            }
            relations.erase(relations.begin() + larger);
        }
    }
    int q;
    std::scanf("%d", &q);
    for(int i = 0; i < q; ++i){
        int s;
        int t;
        std::scanf("%d", &s);
        std::scanf("%d", &t);
        bool find = false;
        for(int j = 0; j < relations.size(); ++j){
            if(relations[j].find(s) != relations[j].end() && relations[j].find(t) != relations[j].end()){
                find = true;
                break;
            }
        }
        if(find){
            std::printf("yes\n");
        } else {
            std::printf("no\n");
        }
    }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int n,m,q,s,t;
int tree[100002];
int sn[100002];
list<int>l[100002];

int slv(int x,int id);

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

  for(int i=0;i<n;i++){
    tree[i]=-1;
    sn[i]=0;
  }
  int id=1;
  for(int i=0;i<n;i++){
    if(tree[i]==-1){
      slv(i,id);
      id++;
    }
  }

  cin>>q;
  for(int i=0;i<q;i++){
    cin>>s>>t;
    if(tree[s]==tree[t])cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }

  return(0);
}

int slv(int x,int id){
  tree[x]=id;
  sn[x]=1;
  for(int i=0;i<l[x].size();i++){
    int y=l[x].front();
    l[x].pop_front();
    l[x].push_back(y);
    if(sn[y]==0){
      slv(y,id);
    }
  }
  return(0);
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int grey=1;
const int white=0;
const int black=-1;

const int maxn=100005;
int head[maxn+5],pos=0;
int color[maxn+5],id[maxn+5];
int n,m;

struct node
{
    int k;
    int next;
};
struct node G[2*maxn+5];

void add(int u,int v)
{
    G[++pos].k=v;
    G[pos].next=head[u];
    head[u]=pos;
}

void bfsvisit(int u,int k)
{
    color[u]=black;
    queue<int> q;
    q.push(u);
    id[u]=k;
    while(!q.empty())
    {
        int r=q.front();
        q.pop();
        for(int j=head[r];j;j=G[j].next)
        {
            int v=G[j].k;
            if(color[v]==white)
            {
                q.push(v);
                color[v]=black;
                id[v]=k;
            }
        }
    }
}

void bfs()
{
    memset(id,0,sizeof(id));
    memset(color,0,sizeof(color));
    int k=0;

    for(int i=0;i<n;i++)
    {
        if(color[i]==white)
        {
            bfsvisit(i,k++);
        }
    }
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }

    bfs();
    int q,s,t;
    scanf("%d",&q);
    while(q--)
    {
        scanf("%d%d",&s,&t);
        if(id[s]==id[t])
            printf("yes\n");
        else printf("no\n");
    }
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX=100000;
static const int NIL=-1;

int n,color[MAX];
vector<int>G[MAX];

void dfs(int r,int c){
  stack<int> S;
  int u,i,v;
  
  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]==NIL){
	color[v]=c;
	S.push(v);
      }
    }
  }
}

void assignColor(){
  int id=1;
  int i,u;
  for(i=0;i<n;i++)color[i]=NIL;
  for(u=0;u<n;u++){
    if(color[u]==NIL)dfs(u,id++);
  }
}

int main(){
  int s,t,m,q;
  int 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;
}