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 <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>
#include <numeric>
#include <queue>

using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if ( i + 1 != list.size() ) {
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


class Node{
public:
    Node(int id) : id(id) 
    {
        // constructer
    }
    void AddFriend(int id){
        this->friends.push_back(id);
    }
    vector<int> Friends() {
        return this->friends;
    }

    int id;
    // bool done = false;
    int clazz = -1;
    vector<int> friends;
};

class List{
public:
    List(int numNode)
    {
        // constructer
        for (int i = 0; i < numNode; i++) {
            this->nodes.insert(std::make_pair(i, Node(i)));
        }
    }
    void SetRelation(int lhs, int rhs)
    {
        this->nodes.at(lhs).AddFriend(rhs);
        this->nodes.at(rhs).AddFriend(lhs);
    }
    int clazz = 1;
    void Traverse(){
        for(int i = 0; i < this->nodes.size(); i++) {
            this->_traverse(i);
            clazz++;
        }
    }
    void _traverse(int id){
        // printf("id %d'%c'\n", id, this->nodes.at(id).clazz);
        if (0 <= this->nodes.at(id).clazz) {
            return;
        }
        this->nodes.at(id).clazz = this->clazz;
        for (auto i : this->nodes.at(id).Friends()) {
            _traverse(i);
        }
    }
    bool Find(int from, int to)
    {
        // printf("find %d - %d\n", from, to);
        // printf("    '%d' : '%d'\n", this->nodes.at(from).clazz, this->nodes.at(to).clazz);
        if(this->nodes.at(from).clazz == -1 || this->nodes.at(to).clazz == -1) return false;
        return this->nodes.at(from).clazz == this->nodes.at(to).clazz;
    }
    // bool _find(int id){
    //     while(!this->queue.empty()){
    //         int i = queue.front();
    //         queue.pop();
    //         if (this->nodes.at(i).done) continue;
    //         for (auto relId : this->nodes.at(i).Friends()) {
    //             if (relId == id) {
    //                 return true;
    //             } else {
    //                 queue.push(relId);
    //             }
    //         }
    //         this->nodes.at(i).done = true;
    //     }
    //     return false;
    // }
    void Print() {
        for (int i = 1; i < nodes.size(); i++) {
            printf("%d %c\n", i, nodes.at(i).clazz);
        }
    }
private:
    std::unordered_map<int, Node> nodes;
    std::queue<int> queue;
};

void solve() {

    int n;
    scanf("%d", &n);
    List list(n);

    int m;
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        int lhs, rhs;
        scanf("%d %d", &lhs, &rhs);
        list.SetRelation(lhs, rhs);
    }
    list.Traverse();
    int k;
    scanf("%d", &k);
    for (int i = 0; i < k; i++) {
        int from, to;
        scanf("%d %d", &from, &to);
        if (list.Find(from, to)) {
            printf("yes\n");
        } else {
            printf("no\n");
        }
    }
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    return 0;
}