Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

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");
  }


}