Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

C++: #include <bits/stdc++.h>

using namespace std;


const int MAX_N = 1000000;
int par[MAX_N];

//syokika
void init(int n){
  for(int i = 0; i < n; i++) par[i] = i;
}

//kinonewomotomeru
int root(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = root(par[x]);
  }
}

//hanntei
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;

  par[x] = y;
}

int main(void){

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

  vector<int> adj[n];

  //syokika
  init(n);

  //heigou
  for(int i = 0; i < m; i++){
    int s, t; cin >> s >> t;
    unite(s, t);
  }

  int q; cin >> q;

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


  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] = join[t];
      
      else{
	join[t]=join[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 <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

using namespace std;

#define MAX_N 100000

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;
bool used[MAX_N];
int cmp[MAX_N];
int V;

void add_edge(int from, int to) {
	G[from].push_back(to);
	rG[to].push_back(from);
}

void dfs(int v) {
	used[v] = true;
	for (int i = 0; i < G[v].size(); ++i)
		if (!used[G[v][i]])
			dfs(G[v][i]);
	vs.push_back(v);
}

void rdfs(int v, int k) {
	used[v] = true;
	cmp[v] = k;
	for (int i = 0; i < rG[v].size(); ++i)
		if (!used[rG[v][i]])
			rdfs(rG[v][i], k);
}

int scc() {
	fill_n(used, MAX_N, false);
	vs.clear();
	for (int v = 0; v < V; ++v)
		if (!used[v])
			dfs(v);
	fill_n(used, MAX_N, false);
	int k = 0;
	for (int i = vs.size() - 1; i >= 0; --i)
		if (!used[vs[i]])
			rdfs(vs[i], k++);
	return k;
}

int main() {
	int n, m;
	cin >> n >> m;
	V = n;
	for (int i = 0; i < m; ++i) {
		int s, t;
		cin >> s >> t;
		add_edge(s, t);
	}
	int k = scc();
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i) {
		int s, t;
		cin >> s >> t;
		fill_n(used, MAX_N, false);
		dfs(s);
		if (used[t])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}

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

bool is_conne(int, int);
int n, m;
std::vector<int> connect_dir[100000]{};
int visited_mother[100000] = {};

int main()
{
	int q, s, t;
	std::cin >> n >> m;
	for (int i = 0; i < m; ++i)
	{
		std::cin >> s >> t;
		connect_dir[s].push_back(t);
		connect_dir[t].push_back(s);
	}
	for (int i = 0; i < n; ++i)
		visited_mother[i] = -1;

	std::cin >> q;
	for (int i = 0; i < q; ++i)
	{
		std::cin >> s >> t;
		if (is_conne(s, t))
			std::cout << "yes" << std::endl;
		else
			std::cout << "no" << std::endl;
	}
	return 0;
}

bool is_conne(int s, int t)
{
	if (visited_mother[s] != visited_mother[t]) return false;
	if (visited_mother[s] != -1) return true;

	visited_mother[s] = s;
	std::vector<int> visited_list{ s };
	int pre_size{ 0 };
	bool flag{};

	while (visited_list.size() != pre_size)
	{
		int now_size = visited_list.size();
		for (int i = pre_size; i < now_size; ++i)
			for (auto e : connect_dir[visited_list[i]])
			{
				if (visited_mother[e] != -1) continue;
				visited_mother[e] = s;
				visited_list.push_back(e);
				if (e == t) flag = true;
			}
		pre_size = now_size;
	}
	if (flag)
		return true;
	return false;
}

C++: #include<iostream>
#include<unordered_map>

C++: #include <iostream>
using namespace std;
  
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j && d[h]!=h){
    j=d[j];
    h=d[h];
}
if(d[j]==j){
    d[j]=h;
    d[a]=h;
}
else{
  d[h]=j;
    d[b]=j;
}
}
cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j || d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<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())
    {
        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)
            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;
}