Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

C++: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); ++i)
#define outl(x) cout<< (x) << '\n'
#define fi first
#define se second
using pii = pair<int,int>;
using ll = long long;

struct UnionFind
{
    int uni[100010];
    UnionFind() { memset(uni, -1, sizeof(uni)); }
    int root(int x) { return uni[x] < 0 ? x : uni[x] = root(uni[x]); }
    bool same(int x, int y) { return (root(x) == root(y)); }
    bool unite(int x, int y) {
        if ((x=root(x)) == (y=root(y)))
            return true;
        if (uni[y] < uni[x])
            swap(x, y);

        uni[x] += uni[y];
        uni[y] = x;
        return (false);
    }
};

UnionFind uf;

int main()
{
   cin.tie(0), ios::sync_with_stdio(false);
   int N, Q, m;

   cin >> N >> m;

   while (m--) {
       int x, y;
       cin >> x >> y;
       uf.unite(x, y);
   }

   cin >> Q;
   while (Q--) {
       int x, y;
       cin >> x >> y;
       outl(uf.same(x,y) ? "yes" : "no");
   }

}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int from[100000];
int to[100000];

bool judge(int f, int t,int n)
{
	bool isFriend = false;
	for (int i = 0; i < n; i++)
	{
		if (from[i] == f)
		{
			if (to[i] == t)
			{
				isFriend = true;
			}
			else
			{
				isFriend = judge(to[i], t, n);
			}
			if (isFriend)
			{
				break;
			}
		}
	}
	return isFriend;
}

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

	for (int i = 0; i < m; i++)
	{
		int f, t;
		cin >> f >> t;

		from[i] = f;
		to[i] = t;
	}

	int q;
	cin >> q;

	for (int i = 0; i < q; i++)
	{
		int f, t;
		cin >> f >> t;

		printf(judge(f, t, n) ? "yes\n" : "no\n");
	}
	return 0;
}

C++: //
//
//  ad-algoD.cpp - created by us162022
//
#include <iostream>
using namespace std;
int find(int join[],int x){
  if(x==join[x])
    return x;

  return find(join,join[x]);
}
int main(int argc, char *argv[])
{
  int n,m,join[100000],num[100000],s,t;
  cin >> n >> m;

  for(int i=0; i<n; i++){
    join[i] = i;
    num[i] = 0;
  }

  for(int i=0; i<m; i++){
    cin >> s >> t;
    s = find(join,s);
    t = find(join,t);

    if(s!=t){
      if(num[s] < num[t])
	join[s] = t;
      
      else{
	join[t]=s;
	if(num[s]==num[t])
	  num[s]++;
      }

    }
  }

  int q;
  cin >> q;

  for(int i=0; i<q; i++){
    cin >> s >> t;

    if(find(join,s)==find(join,t))
      cout << "yes\n";

    else
      cout << "no\n";
  }
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NTL = -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]==NTL){
      color[v]=c;
      S.push(v);
      }
    }
  }
}

void assigncolor() {
  int id=1;
  for(int i=0;i<n;i++) color[i]=NTL;
  for(int u =0;u<n;u++){
    if(color[u]==NTL)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;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
ll mod = 1000000007;

const int MAX = 100000;
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();
        rep(i, G[u].size()) {
            int v = G[u][i];
            if(color[v] == NIL) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}

void assignColor() {
    int id = 1;
    rep(i, n) {
        color[i] = NIL;
    }
    rep(u, n) {
        if(color[u] == NIL) {
            dfs(u, id++);
        }
    }
}
 
int main() {
    int s, t, m, q;

    cin >> n >> m;

    rep(i, m) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assignColor();

    cin >> q;

    rep(i, q) {
        cin >> s >> t;
        if(color[s] == color[t]) {
            cout << "yes" << endl;
        }
        else {
            cout << "no" << endl;
        }
    }
    
    return 0;
}

C++: #include <cstdint>
#include <vector>

using namespace std;

#define GRAPH_SIZE 100000

enum state_t {
  WHITE,
  GRAY,
  BLACK,
};

struct vertex_t {
  uint32_t color;
  vector<uint32_t> connections;
};

static vertex_t graph[GRAPH_SIZE];
static state_t state[GRAPH_SIZE];

static void colorize(uint32_t index, uint32_t color)
{
  if (state[index] == WHITE) {
    state[index] = GRAY;
    graph[index].color = color;
    for (uint32_t i = 0; i < graph[i].connections.size(); i++) {
      colorize(graph[i].connections[i], color);
    }
    state[index] = BLACK;
  }
}

void check_connection()
{
  for (uint32_t i = 0; i < GRAPH_SIZE; i++) {
    state[i] = WHITE;
  }

  uint32_t color = 0;
  for (uint32_t i = 0; GRAPH_SIZE; i++) {
    colorize(i, color);
    color++;
  }
}

#include <iostream>

int32_t main()
{
  uint32_t n, m;
  cin >> n >> m;

  for (uint32_t i = 0; i < m; i++) {
    uint32_t index, to;
    cin >> index >> to;
    graph[index].connections.push_back(to);
  }

  check_connection();

  uint32_t qs;
  cin >> qs;

  for (uint32_t i = 0; i < qs; i++) {
    uint32_t from, to;
    cin >> from >> to;

    if (graph[from].color == graph[to].color) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  return 0;
}