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 <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;
    d[b]=h;

}
else{
  d[h]=j;
    d[a]=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 <bits/stdc++.h>
using namespace std;
int f[100005];
int find(int x){
	int xx=x;
	while(f[x]!=x)x=f[x];
	while(f[xx]!=x){
		int tem=f[xx];
		f[xx]=x;
		xx=tem;
	}
	return x;
}
void join(int x,int y){
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)f[fx]=fy;
}
int n,m;
int main(){
	cin>>n>>m;
	for(int i=0;i<=n;i++)f[i]=i;
	int a,b;
	while(m--){
		cin>>a>>b;
		join(a,b);
	}
	int q;cin>>q;
	while(q--){
		cin>>a>>b;
		if(find(a)==find(b)){
			cout<<"yes"<<endl;
		}else cout<<"no"<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>

struct node {
	int parent;
	std::vector<int> relation;
};

void dfs(std::vector<node> &n) {
	std::stack<int> s;
	std::vector<int> d(n.size()), dd(n.size());
	for (size_t i = 0; i < n.size(); i++) {
		if (dd[i] == 1)continue;
		n[i].parent = -1;
		std::fill(d.begin(), d.end(), 0);
		s.push(i);
		while (s.size() != 0) {
			int num = s.top(); s.pop();
			d[num] = 1;
			dd[num] = 1;
			for (std::vector<int>::iterator it = n[num].relation.begin(); it != n[num].relation.end(); it++) {
				if (d[*it] == 1) {
					n[num].relation.erase(it);
					it--;
				} else {
					s.push(*it);
					d[*it] = 1;
					n[*it].parent = num;
					std::vector<int>::iterator itt= std::find(n[*it].relation.begin(), n[*it].relation.end(), num);
					if (itt != n[*it].relation.end()) {
						n[*it].relation.erase(itt);
					}
				}
			}
		}
	}
}

void makeparent(std::vector<node> &n) {
	std::queue<int> q;
	for (size_t i = 0; i < n.size(); i++) {
		n[i].parent = -1;
	}
	for (size_t i = 0; i < n.size(); i++) {
		q.push(i);
		int parent = i;
		while (q.size() != 0) {
			parent = q.front(); q.pop();
			for (size_t j = 0; j < n[parent].relation.size(); j++) {
				q.push(n[parent].relation[j]);
				n[n[parent].relation[j]].parent = parent;
			}
		}
	}
}

bool isconnect(std::vector<node> n, int from, int to) {
	std::stack<int> s;
	while (n[from].parent != -1) {
		from = n[from].parent;
		if (from == to) return true;
	}
	s.push(from);
	while(s.size() != 0) {
		from = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[from].relation.begin(); it != n[from].relation.end(); it++) {
			if (to == *it) return true;
			s.push(*it);
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<node> n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].relation.push_back(to);
		n[to].relation.push_back(from);
	}

	dfs(n);
	//makeparent(n);

	/*
	for (size_t i = 0; i < n.size(); i++) {
	std::cout << i << " " << n[i].parent << std::endl;
	}
	*/
	/*
	for (size_t i = 0; i < n.size(); i++) {
		for (size_t j = 0; j < n[i].relation.size(); j++) {
			std::cout << i << n[i].relation[j] << std::endl;
		}
	}
	//*/

	int question;
	std::cin >> question;

	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (isconnect(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;

int find(vector<vector<int>> &frnd, vector<int> &state, int u){
    for (int i = 0; i < frnd[u].size(); i++){
        if (state[frnd[u][i]] == 0){
            return frnd[u][i];
        }
    }
    return -1;
}

int main(){
    int n, m;
    cin >> n >> m;
    vector<vector<int>> frnd(n);
    int s, t;
    for (int i = 0; i < m; i++){
        cin >> s >> t;
        frnd[s].push_back(t);
        frnd[t].push_back(s);
    }
    vector<int> color(n, 0);
    int cl = 1;
    for (int i = 0; i < n; i++){
        stack<int> s;
        //vector<int> state(n, 0);
        if (color[i] == 0){
            s.push(i);
            color[i] = cl;
            //state[i]++;
            while(!s.empty()){
                int v = s.top();

                s.pop();
                for (int j = 0; j < frnd[v].size(); j++){
                    int u = frnd[v][j];
                    if (color[u] == 0){
                        color[u] = cl;
                        s.push(u);
                    }
                }
                /*int nxt = find(frnd, state, v);
                if (nxt != -1){
                    s.push(nxt);
                    color[nxt] = cl;
                    state[nxt]++;
                }
                else{
                    state[v]++;
                    s.pop();
                }*/
            }
        }
        cl++;
    }
    int q;
    cin >> q;
    int a, b;
    for (int i = 0; i < q; i++){
        cin >> a >> b;
        if (color[a] == color[b])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}