Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func newGraph(nNodes int) *Graph {
	g := Graph{}
	g.nodes = make([]*GraphNode, nNodes)
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode] = &GraphNode{}
		g.nodes[iNode].key = GraphNodeKey(iNode)
	}
	return &g
}

var infty = 1000000000

type GraphNodeKey int

// GraphNode is a node for undirected graph
type GraphNode struct {
	key       GraphNodeKey
	adjacents []*GraphNode
	visited   bool
	d, f      int
	parent    *GraphNode
	cc        int
}

func (g *Graph) setAdjacent(l, r GraphNodeKey) {
	g.nodes[l].adjacents = append(g.nodes[l].adjacents, g.nodes[r])
}

// Graph is a undirected graph
type Graph struct {
	nodes []*GraphNode
}

func (g *Graph) initialize() {
	for iNode := 0; iNode < len(g.nodes); iNode++ {
		g.nodes[iNode].visited = false
		g.nodes[iNode].d = infty
		g.nodes[iNode].f = infty
		g.nodes[iNode].parent = nil
		g.nodes[iNode].cc = -1 // 22.3-12
	}
}

type QueueEntry struct {
	node *GraphNode
	next *QueueEntry
}

type Queue struct {
	head, tail *QueueEntry
}

func (q *Queue) enqueue(node *GraphNode) {
	entry := QueueEntry{}
	entry.node = node

	if q.tail == nil {
		q.head = &entry
		q.tail = &entry
		return
	}

	q.tail.next = &entry
	q.tail = q.tail.next
}

func (q *Queue) dequeue() *QueueEntry {
	if q.head == nil {
		return nil
	}
	poped := q.head
	q.head = q.head.next
	if q.head == nil {
		q.tail = nil
	}
	return poped
}

func (q *Queue) isEmpty() bool {
	return q.head == nil
}

var time = 0

func fillDistancesByDepthFirstSearch(g *Graph) {
	g.initialize()
	time = 0
	cc := 0

	for _, u := range g.nodes {
		if !u.visited {
			dfsVisit(g, u, cc)
			cc++
		}
	}
}

func dfsVisit(g *Graph, u *GraphNode, cc int) {
	time++
	u.d = time
	u.cc = cc

	u.visited = true
	for _, v := range u.adjacents {
		if !v.visited {
			v.parent = u
			dfsVisit(g, v, cc)
		}
	}
	time++
	u.f = time
}

func main() {
	scanner.Split(bufio.ScanWords)

	nUsers := nextInt()
	nEdges := nextInt()
	graph := newGraph(nUsers)
	for iEdge := 0; iEdge < nEdges; iEdge++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())
		graph.setAdjacent(l, r)
		graph.setAdjacent(r, l)
	}

	fillDistancesByDepthFirstSearch(graph)

	nQueries := nextInt()
	for iQuery := 0; iQuery < nQueries; iQuery++ {
		l := GraphNodeKey(nextInt())
		r := GraphNodeKey(nextInt())

		// fmt.Println(graph.nodes[l].d, graph.nodes[r].d, graph.nodes[r].f, graph.nodes[l].f)
		if graph.nodes[l].cc == graph.nodes[r].cc {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

class Relation{
public:
	Relation(int tmp_n,int tmp_m){
		n = tmp_n;
		m = tmp_m;
		V = new vector<int>[n];
		group = new int[n];
		for(int i=0;i<n;i++){
			group[i] = 0;
		}
		group_number = 1;
	}

	void push(int s,int t){
		V[s].push_back(t);
		V[t].push_back(s);
	}

	void makeGroup(){
		int node;
		queue<int> Q;
		for(int i=0;i<n;i++){
			if(group[i] == 0){
				group[i] = group_number;
				Q.push(i);
				while(!Q.empty()){
					node = Q.front();
					Q.pop();
					if(V[node].size() > 0){
						for(int k=0; k < V[node].size();k++){
							if(group[V[node][k]] == 0){
								group[V[node][k]] = group[node];
								Q.push(V[node][k]);
							}
						}
					}
				}
				group_number++;
			}
		}
	}

	bool canAccsess(int s,int t){
		return group[s] == group[t];
	}

private:
	vector<int>* V;
	int* group;
	int group_number;
	int n,m;
};

int main(){
	int n,m,s,t,q;
	scanf("%d %d",&n,&m);

	Relation relation(n,m);
	for(int i=0;i<m;i++){
		scanf("%d %d",&s,&t);
		relation.push(s,t);
	}
	relation.makeGroup();
	scanf("%d",&q);
	for(int i=0;i<q;i++){
		scanf("%d %d",&s,&t);
		if(relation.canAccsess(s,t)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
    return 0;
}

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

class Union{
public:
	Union():id(++n), group(this){};
	Union* get_u(){
		if (group == this){
			return group;
		}else{
			return (group = (group->get_u()));
		}
	}
	int get_id(){
		return id;
	}
	void set_u(Union &other){
		group = other.get_u();
	}
	void join(Union &other){
		get_u()->set_u(other);
	}
	bool question(Union &other){
		return get_u() == other.get_u();
	}
private:
	int id;
	Union* group;
	static int n;
};
int Union::n{-1};
int main(){
	int n, m;
	std::cin >> n >> m;
	std::vector<Union> vec(n);
	int a, b;
	for (auto i = 0; i < m; ++i){
		std::cin >> a >> b;
		vec.at(a).join(vec.at(b));
	}
	int q;
	std::cin >> q;
	for (auto i = 0; i < q; ++i){
		std::cin >> a >> b;
		if (vec.at(a).question(vec.at(b))){
			std::cout << "yes\n";
		}else{
			std::cout << "no\n";
		}
	}
}

C++: #include <vector>
#include <cstdio>
class union_find{
????????std::vector<int> parent;
public:
????????int root(int a){return parent[a]==a?a:(parent[a]=root(parent[a]));}
????????union_find(int n):parent(n+1){for(int i=1;i<=n;i++)parent[i]=i;}
????????int same(int a,int b){return root(a)==root(b);}
????????int unite(int a,int b){
????????????????int x=root(a),y=root(b);//if(x==y)return 0;
????????????????parent[x]=y;
????????????????return 1;
????????}
};
int main(){
????????int n,m;
????????scanf("%d%d",&n,&m);
????????union_find uf(n);
????????int a,b;
????????for(;m--;uf.unite(a,b))scanf("%d%d",&a,&b);
????????for(scanf("%d",&m);m--;puts(uf.same(a,b)?"yes":"no"))scanf("%d%d",&a,&b);
}

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

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

int n,m,q,s,t,z[100000];
vector<pair<int,int>> w;

void dfs(int n,int c) {
    z[n] = c;
    auto r = lower_bound(w.begin(),w.end(),make_pair(n,0));
    auto l = lower_bound(w.begin(),w.end(),make_pair(n+1,0));
    for(auto itr = r;itr != l;itr++) if(z[itr->second] != c)dfs(itr->second,c);
}

int main() {
    for(int i = 0;i < 100000;i++)z[i] = -1;
    cin >> n >> m;
    for(int i = 0;i < m;i++) {
        cin >> s >> t;
        w.push_back(make_pair(s,t));
        w.push_back(make_pair(t,s));
    }
    sort(w.begin(),w.end());
    cin >> q;
    int cc = 1;
    for(int i = 0;i < q;i++) {
        cin >> s >> t;
        if(z[s] == -1) dfs(s,cc++);
        if(z[s] == z[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

C++: #include <cstdio>
#include <vector>
#include <cstring>

bool isFriend(std::vector<int> *a, int s, int t, int *checked, int c){
    int tmp;
    checked[s] = c;
    if(s==t)return true;
    for(int i=0;i<a[s].size();i++){
        tmp = a[s][i];
        if(checked[tmp]>=c)continue;
        if(isFriend(a,tmp,t,checked,c))return true;
    }
    return false;
}

int main(){
    int i, n, m, q, s, t;
    int *checked;
    scanf("%d %d",&n,&m);
    checked = (int *)malloc(sizeof(int)*n);
    memset(checked, 0, sizeof(int)*n);
    std::vector<int> a[n];
    for(i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        a[s].push_back(t);
        a[t].push_back(s);
    }
    scanf("%d",&q);
    for(i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        if(isFriend(a,s,t,checked,i+1))printf("yes\n");
        else printf("no\n");
    }
    return 0;
}