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 <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
typedef long long ll;

// union - find tree !!!!!!!!!!!!!!!!!!!!!!!!!
vector<int> par; //oya
vector<int> rnk; //ki no hu ka sa
// n????´???§?????????
void init(int n){
    par.resize(n);rnk.resize(n);
    FOR(i, 0, n){par[i] = i;rnk[i] = 0;}
}
//??¨??????????±???????
int find(int x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}
//x??¨y????±??????????????????????
void Merge(int x, int y){
    x = find(x);y = find(y);
    if(x == y) return;
    if(rnk[x] < rnk[y])par[x] = y;
    else{par[y] = x;if(rnk[x] == rnk[y]) rnk[x]++;}
    return;
}

int main(){
  int n, m; cin >> n >> m;
  init(n);
  FOR(i,0,m){
    int a, b;
    cin >> a >> b;
    Merge(a, b);
  }
  int q;
  cin >> q;
  FOR(i,0,q){
    int a, b;
    cin >> a >> b;
    if(find(a) == find(b)){
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF INT_MAX

vector< vector<int> > adj;
vector<int> serial;

void dfs(int v, int sn)
{
    serial[v] = sn;
    for (auto e : adj[v]) {
        if (serial[e]) continue;
        dfs(e, sn);
    }
}

int main(int, char**)
{
    int n, m;
    cin >> n >> m;

    adj.assign(n, vector<int>());
    int s, t;
    rep(i,m) {
        cin >> s >> t;
        adj[s].push_back(t);
        adj[t].push_back(s);
    }


    serial.assign(n, 0);
    int sn = 1;
    rep(i,n) {
        if (serial[i]) continue;
        dfs(i, sn++);
    }
    int q;
    cin >> q;
    rep(i,q) {
        cin >> s >> t;
        if (serial[s] == serial[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <vector>
using namespace std;
#define MAX 100000

vector<int> G[MAX];
int color[MAX];
int c = 0;

void dfs(int v) {
    color[v] = c;
    for (int i = 0; i < G[v].size(); i++) {
        if (color[G[v][i]] == 0) dfs(G[v][i]);
    }
}

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

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

    for (i = 0; i < n; i++) color[i] = 0;

    for (i = 0; i < n; i++) {
        if (color[i] == 0) {
            c++;
            dfs(i);
        }
    }

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

    return 0;
}

C++: #include"bits/stdc++.h"
using namespace std;

#define NIL -1

struct Node {
	int type = NIL;
	bool flag = false;
	vector<int> v;
};

void dfs(vector<Node>& u, int id, int type)
{
	if (u[id].flag) return;

	u[id].flag = true;
	u[id].type = type;
	for (int i = 0; i < u[id].v.size(); i++) dfs(u, u[id].v[i], type);

	return;
}

int main(void)
{
	int i, N, m, l, r, q, type = 0;
	cin >> N >> m;
	vector<Node> u(N);

	for (i = 0; i < m; i++)
	{
		cin >> l >> r;
		u[l].v.push_back(r);
		u[r].v.push_back(l);
	}

	for (i = 0; i < N; i++)
	{
		dfs(u, i, type);
		type++;
	}

	cin >> q;
	for (i = 0; i < q; i++)
	{
		cin >> l >> r;
		cout << ((u[l].type == u[r].type) ? "yes" : "no") << endl;
	}

	return 0;
}