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 <bits/stdc++.h>
#include <stdint.h>

typedef uint32_t u32;

template<u32 N_>
class UnionFindTree {
private:
  u32 par[N_];
  u32 rank[N_];
public:
  UnionFindTree(u32 n) {
    assert(n < N_);
    for(u32 i = 0; i < n; ++i) {
      par[i] = i;
      rank[i] = 0;
    }
    return;
  }
  int find(u32 x) {
    if( par[x] == x ) return x;
    return par[x] = find(par[x]);
  }
  void unite(u32 x, u32 y) {
    x = find(x);
    y = find(y);
    if( x == y ) return;
    if( rank[x] < rank[y] ) {
      par[x] = y;
      return;
    }
    par[y] = x;
    if( rank[x] == rank[y] ) rank[x] += 1;
    return;
  }  
};

int main() {

  u32 n, m;
  std::cin >> n >> m;

  UnionFindTree<200000> uft(n);

  for(u32 i = 0; i < m; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    uft.unite(x, y);
  }

  u32 q;
  std::cin >> q;

  for(u32 i = 0; i < q; ++i) {
    u32 x, y;
    std::cin >> x >> y;
    std::cout << (uft.find(x) == uft.find(y) ? "yes" : "no") << std::endl;
  }
  
  return 0;
}

C++: #include <vector>
#include <iostream>
using namespace std;
int n, m, q, x, y, t; int group[111111]; vector<int> f[111111];
int main() {
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++) group[i] = i, f[i].push_back(i);
	for(int i = 0; i < m; i++) {
		scanf("%d%d", &x, &y);
		if(group[x] == group[y]) continue;
		x = group[x], y = group[y], t = f[y].size();
		if(f[y].size() > f[x].size()) swap(x, y);
		for(int j = 0; j < t; j++) {
			group[f[y].back()] = group[x];
			f[x].push_back(f[y].back());
			f[y].pop_back();
		}
	}
	scanf("%d", &q);
	for(int i = 0; i < q; i++) {
		scanf("%d%d", &x, &y);
		printf("%s\n", group[x] == group[y] ? "yes" : "no");
	}
	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define MAX (int)1e5

vector<int> G[MAX];
queue<int> qu;

int main() {
	int n, m, q;
	int ind,tmp;
	cin >> n >> m;
	bool flag = 0;
	for (int i = 0; i < m; i++) {
		cin >> ind >> tmp;
		G[ind].push_back(tmp);
	}
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> ind >> tmp;
		flag = 0;
		qu.push(ind);
		while (not qu.empty()) {
			int u = qu.front(); qu.pop();
			if (u == tmp) {
				flag = 1;
				break;
			}
			for (int j = 0; j < G[u].size(); j++) {
				qu.push(G[u][j]);
			}
		}
		if (not flag) {
			swap(ind, tmp);
			qu.push(ind);
			while (not qu.empty()) {
				int u = qu.front(); qu.pop();
				if (u == tmp) {
					flag = 1;
					break;
				}
				for (int j = 0; j < G[u].size(); j++) {
					qu.push(G[u][j]);
				}
			}
		}
		if (flag) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,m,i,s,t,p[1<<17],q;
int root(int u){
  return p[u]==u?u:p[u]=root(p[u]);
}
int main(void){
  for(i=0;i<1<<17;i++)p[i]=i;
  cin>>n>>m;
  while(m--){
    cin>>s>>t;
    s=root(s);
    t=root(t);
    // if(s>t)swap(s,t);
    if(s!=t)p[t]=s;
  }
  cin>>q;
  while(q--){
    cin>>s>>t;
    cout<<(root(s)==root(t)?"yes":"no")<<endl;
  }
  return 0;
}

C++: #include <stdio.h>

int m[100000] = {0};

void init() {
	int i;
	for ( i = 0; i < 100000; ++i) {
		m[i] = i;
	}
}

int find(int a) {
	if ( a == m[a]) {
		return a;
	}
	return m[a] = find(m[a]);
}

int friend(int x, int y) {
	if ( find(x) == find(y)) {
		return 1;
	} else {
		return 0;
	}
}

void unit (int x, int y) {
	x = find(x);
	y = find(y);
	
	if ( x == y ) {
		return ;
	}
	m[y] = x;
}

int main() {
	int n, s, t, u, v, i;
	init();
	scanf("%d", &n);
	scanf("%d", &s);
	
	for ( i = 0; i < s; ++i) {
		scanf("%d", &u);
		scanf("%d", &v);
		unit( u, v);
	}
	
	scanf("%d", &t);
	
	for ( i = 0; i < s; i++) {
		scanf("%d", &u);
		scanf("%d", &v);
		if ( friend( u, v)) {
			printf("yes\n");
		} else {
			printf("no\n");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int flag[100000];
vector<int> G[100000];
int color = 0;

void dfs(int f)
{
    for (int i = 0; i < G[f].size(); i++)
    {
        if (flag[G[f][i]] == -1)
        {
            flag[G[f][i]] = color;
            dfs(G[f][i]);
        }
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    int f, g;
    for (int i = 0; i < m; i++)
    {

        cin >> f >> g;
        G[f].push_back(g);
        G[g].push_back(f);
    }

    for (int i = 0; i < n; i++)
        flag[i] = -1;

    for (int i = 0; i < n; i++)
    {
        if (flag[i] == -1)
        {
            color = i;
            flag[i] = color;
            dfs(i);
        }
    }
    int q;
    cin >> q;
    while (q--)
    {
        cin >> f >> g;
        if (flag[f] == flag[g])
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
}