Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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

C++: #include <iostream>
#include <cstdio>
#include <queue>

#define MAX 100000


using namespace std;


int gcol[MAX];

int main(void)
{
	int n;
	int m;
	int s;
	int t;
	int q;
	int type = 1;



	cin >> n >> m;

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

		if (gcol[s] != 0) {
			gcol[t] = gcol[s];
		}
		else if (gcol[t] != 0) {
			gcol[s] = gcol[t];
		}
		else {
			gcol[s] = gcol[t] = type;
			type++;
		}
	}

	/*
	for (int i = 0; i < m; i++) {
		cout << "gcol[" << i << "] = " << gcol[i] << endl;
	}
	*/

	cin >> q;

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

		if (gcol[s] == gcol[t] && gcol[s] != 0) {
			cout << "yes";
		}
		else {
			cout << "no";
		}

		cout << 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 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 <stdio.h>
#include <vector>
#include <queue>
using namespace std;

#define MAX_N (100005)
int n, m;
vector<int> G[MAX_N];
int Group[MAX_N];

int main()
{
    int id = 0;

    scanf("%d %d", &n, &m);
    
    for ( int i = 0; i < m; i++ ) {
	int s, t;
	scanf("%d %d", &s, &t);
	G[s].push_back(t);
	G[t].push_back(s);
    }

    for ( int i = 0; i < n; i++ ) {
	if ( Group[i] == 0 ) {
	    id++;
	    queue<int> q;
	    q.push(i);
	    Group[i] = id;
	    while ( !q.empty() ) {
		int cur = q.front(); q.pop();
		for ( int j = 0; j < G[cur].size(); j++ ) {
		    int fri = G[cur][j];
		    if ( Group[fri] == 0 ) {
			Group[fri] = id;
			q.push(fri);
		    }
		}
	    }
	}
    }

    int q;
    scanf("%d", &q);

    for ( int i = 0; i < q; i++ ) {
	int s, t;
	scanf("%d %d", &s, &t);
	if ( Group[s] == Group[t] ) {
	    printf("yes\n");
	}
	else {
	    printf("no\n");
	}
    }

    return 0;
}