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;

struct Unionfind {
	vector<int> data;

	Unionfind(int n) { data.assign(n, -1); }

	int root(int n) {
		if (data[n] < 0) return (n);
		return (data[n] = root(data[n]));
	}

	int size(int n) { return (-data[root(n)]); };
	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;
		if (data[x] > data[y]) swap(x, y);
		data[x] += data[y];
		data[y] = x;
	}
};

int main() {
    int N, M; cin >> N >> M;
    Unionfind uni(N);
    for (int i = 0; i < M; i++) {
        int a, b; cin >> a >> b;
        uni.unite(a, b);
    }
    int Q; cin >> Q;
    while (Q--) {
        int a, b; cin >> a >> b;
        cout << (uni.same(a, b) ? "yes" : "no") << '\n';
    }
    return (0);
}

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

using namespace std; 

int noc;
vector<int> comp;
vector< vector<int> > adj;

void dfs(int v) {
	comp[v] = noc;
	for (int i = 0; i < adj[v].size(); ++i) {
		if (comp[adj[v][i]] < 0) {
			dfs(adj[v][i]);
		}
	}
	return;
}

int main(void) {
	int nov,noe,u,v;
	cin >> nov >> noe;
	adj = vector< vector<int> >(nov);
	for (int i = 0; i < noe; ++i) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	noc = 0;
	comp = vector<int>(nov,-1);
	for (int i = 0; i < nov; ++i) {
		if (comp[i] < 0) {
			++noc;
			dfs(i);
		}
	}

	int noq;
	cin >> noq;
	for (int q = 0; q < noq; ++q) {
		cin >> u >> v;
		if (comp[u] == comp[v]) {
			cout << "yes" << endl;
		}
		else {
			cout << "no" << endl;
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100000;

class UnionFind {
private:
    vector<int> par;
public:
    UnionFind(){}
    UnionFind(const int node_size) : par(node_size){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    bool unite(int x, int y){
        int z;
        while(par[x] != par[y]){
            if(par[x] > par[y]) swap(x, y);
            z = par[x], par[x] = par[y];
            if(y == z) return false;
            x = z;
        }
        return true;
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

int main()
{
    int n,m,q;
    int a,b;
    cin >> n >> m;
    UnionFind uf(n);
    for(int i=0;i<m;i++){
        cin >> a >> b;
        uf.unite(a,b);
    }
    cin >> q;
    for(int i=0;i<q;i++){
        cin >> a >> b;
        if(uf.same(a,b)){
            cout << "yes\n";
        }else{
            cout << "no\n";
        }
    }
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 100000

struct Node{
	int group;
	queue<int> fr;
};

int N, M, Q, group_num = 0;
Node person[MAX];

void isFriend(int a, int b){
	if(person[a].group < 0 && person[b].group < 0){
		int p = a, next, m;
		queue<int> move;
		group_num++;
		person[p].group = group_num;
		//cout << a << " is group " << person[a].group << endl;
		move.push(p);

		while(!move.empty()){
			p = move.front();
			if(person[p].fr.empty()){
				move.pop();
			} else {
				m = person[p].fr.size();
				while(m--){
					next = person[p].fr.front();
					person[p].fr.pop();
					if(person[next].group < 0){
						person[next].group = group_num;
						//cout << next << " is group " << person[next].group << endl;
						move.push(next);
					}
				}
				move.pop();
			}
		}

		if(person[a].group == person[b].group){
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	} else if(person[a].group == person[b].group){
		cout << "yes" << endl;
	} else {
		cout << "no" << endl;
	}
}

int main(){
	cin >> N >> M;
	for(int i = 0; i < N; i++)
		person[i].group = -1;
	while(M--){
		int a, b;
		cin >> a >> b;
		person[a].fr.push(b);
		person[b].fr.push(a);
	}
	cin >> Q;
	while(Q--){
		int a, b;
		cin >> a >> b;
		isFriend(a, b);
	}
	return 0;
}

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

using namespace std;

// const int N = 100000;
vector<vector<int>> A;
vector<bool> visited;
// int n;

bool dfs(int s, int t) {
    stack<int> S;
    S.push(s);
    visited[s] = true;

    while (!S.empty()) {
        auto j = S.top();
        S.pop();

        auto& v = A[j];
        for (auto it = v.begin(); it != v.end(); ++it) {
            auto i = *it;
            if (i == t) {
                return true;
            }
            if (!visited[i]) {
                visited[j] = true;
                S.push(i);
            }
        }
    }
    return false;
}

int main() {
    int n;
    int m;
    cin >> n >> m;
    A.resize(n);
    visited.resize(n);
    for (int i = 0; i < m; ++i) {
        int s, t;
        cin >> s >> t;
        A[s].push_back(t);
        A[t].push_back(s);
    }

    int q;
    cin >> q;
    for (int j = 0; j < q; ++j) {
        int x, y;
        cin >> x >> y;

        if (dfs(x, y)) {
            cout << "yes";
        } else {
            cout << "no";
        }
        cout << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<list>
#define MAX 100000
using namespace std;

class person
{
public:
	bool Isdone = false;
	list <person*> lis;
	bool serch(person* Per)
	{
		Isdone = true;
		while (!lis.empty())
		{
			person* temp = lis.front();
			lis.pop_front();
			if (temp->Isdone == true)continue;
			if (temp == Per)return true;
			else
			{
				if (temp->serch(Per))return true;
			}
		}
		return false;
	}
};

int main()
{
	person *P;
	P = new person[MAX + 1];
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		P[p1].lis.push_back(&P[p2]);
		P[p2].lis.push_back(&P[p1]);
	}
	int q;
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int p1, p2;
		cin >> p1 >> p2;
		if (P[p1].serch(&P[p2]))
		{
			cout << "yes" << endl;
		}
		else
		{
			cout << "no" << endl;
		}
		for (int l = 0; l < n; l++)P[l].Isdone = false;
	}
	delete[] P;
	return 0;
}