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 <stack>
#include <vector>
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);
      }
    }
  }
}

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

  cin>>n>>m;
  for(int i=0;i<n;i++){
    color[i]=NIL;
  }

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

  for(int u=0;u<n;u++){
    int id=1;

    if(color[u]==NIL){
      dfs(u,id++);
    }
  }

  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 <bits/stdc++.h>
using namespace std;
#define MAX 100005

vector<vector<int> > G, R;
vector<int> ve;
queue<int> Q;
int choose[MAX];
int n, m, i, j;

int main(void) {
	cin >> n >> m;
	G.resize(n);
	int a, b;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		G[a].push_back(b);
		G[b].push_back(a);
	}
	for (i = 0; i < n; i++) {
		choose[i] = 0;
	}
	for (i = 0; i < n; i++) {
		if (choose[i] != 0)
			continue;
		Q.push(i);
		ve.clear();
		ve.push_back(i);
		choose[i] = 1;
		int now, cnt, next;
		while (!Q.empty()) {
			now = Q.front();
			Q.pop();
			for (cnt = 0; cnt < G[now].size(); cnt++) {
				next = G[now][cnt];
				if (choose[next] == 0) {
					Q.push(next);
					ve.push_back(next);
					choose[next] = 1;
				}
			}
		}
		R.push_back(ve);
		int dis = R.size() - 1;
		sort(R[dis].begin(), R[dis].end());
	}
	cin >> m;
	for (i = 0; i < m; i++) {
		cin >> a >> b;
		for (j = 0; j < R.size(); j++) {
			if (*(lower_bound(R[j].begin(), R[j].end(), a)) == a) {
				if (*(lower_bound(R[j].begin(), R[j].end(), b)) == b) 
					cout << "yes" << endl;
				else
					cout << "no" << endl;
			}
		}
	}
}

C++: #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T>
inline bool chmin(T& a, T b) {
   if (a > b) {
      a = b;
      return true;
   }
   return false;
}
template <class T>
inline bool chmax(T& a, T b) {
   if (a < b) {
      a = b;
      return true;
   }
   return false;
}
void hr() {
   cerr << "--------------" << endl;
}
const int INF = 1001001001;
const int MOD = 1000000007;

vector<int> to[100000];
int color[100000];
void dfs(int u, int c) {
   color[u] = c;
   for (int v : to[u]) {
      if (color[v] == INF) dfs(v, c);
   }
}

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

   int n, m;
   cin >> n >> m;
   rep(i, 0, m) {
      int s, t;
      cin >> s >> t;
      to[s].push_back(t);
      to[t].push_back(s);
   }
   rep(i, 0, n) color[i] = INF;
   int c = 0;
   rep(i, 0, n) {
      if (color[i] == INF) {
         dfs(i, c++);
      }
   }
   int q;
   cin >> q;
   rep(i, 0, q) {
      int s, t;
      cin >> s >> t;
      if (color[s] == color[t])
         cout << "yes" << endl;
      else
         cout << "no" << endl;
   }
   return 0;
}

C++: #include <iostream>
#include <queue>
#include <vector>
using namespace std;
    queue<int> Q;
vector<int> u[500000];
int cnt=0,color[500000];
// white 0 gray 1 black 2
// void BFS(int s,int n){
//     if(color[s]==0){
//         cnt++;
//         color[s]=cnt;
//         Q.push(s);
//         while(Q.size()!=0){
//             int tmp=Q.front();
//             Q.pop();
//             for(int j=0;j<u[tmp].size();j++){
//                 int to = u[tmp][j];
//                 if(color[to]==0){
//                     color[to]=cnt;
//                     Q.push(to);
//                 }
//             }
//         }
//     }
// }
int main(){
    int n,m,s,t,q;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        cin>>s>>t;
        u[s].push_back(t); 
        u[t].push_back(s);
    }
    cin>>q;
    for(int s=0;s<n;s++){
        if(color[s]==0){
            cnt++;
            color[s]=cnt;
            Q.push(s);
            while(Q.size()!=0){
                int tmp=Q.front();
                Q.pop();
                for(int j=0;j<u[tmp].size();j++){
                    int to = u[tmp][j];
                    if(color[to]==0){
                        color[to]=cnt;
                        Q.push(to);
                    }
                }
            }
    }
    }
    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 <cstring>
#include <cstdio>
using namespace std;

int fri[100010];

int find(int x){
  if( fri[x] == x ) return x;
  else return fri[x] = find(fri[x]);
}

void uf_union(int x, int y){
  x = find(x);
  y = find(y);
  fri[x] = y;
}

int main(){

  int n,m,q;
  cin >> n >> m;

  // init
  for(int i=0; i<n; i++) fri[i] = i;

  //
  for(int i=0; i<m; i++){
    int u,v; scanf("%d %d",&u,&v);
    uf_union(u,v);
  }

  cin >> q;
  for(int i=0; i<q; i++){
    int u,v; scanf("%d %d",&u,&v);
    if( find(u) == find(v) ) printf("yes\n");
    else printf("no\n");
  }


}