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 <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int MAX = 100000;
static const int NIL = -1;
int n, m;
vector<int> G[MAX];
int color[MAX];

void dfs(int r , int col) {
	stack<int> s;
	s.push(r);
	color[r] = col;
	while (!s.empty()) {
		int u = s.top();
		s.pop();
		int i;
		rep(i, G[u].size()) {
			int v = G[u][i];
			if (color[v] == NIL) {
				color[v] = col;
				s.push(v);
			}
		}
	}
}

void assignCol() {
	int col = 1;
	int i;
	rep(i, n) color[i] = NIL;
	rep(i, n) {
		if (color[i] == NIL)
			dfs(i,col++);
	}
}

signed main() {
	ios::sync_with_stdio(false);
	int q;
	cin >> n >> m;
	int i,s,t;
	rep(i, m) {
		cin >> s >> t;
		G[s].pb(t);
		G[t].pb(s);
	}
	assignCol();
	cin >> q;
	rep(i, q) {
		cin >> s >> t;
		if (color[s] == color[t])
			cout << "yes\n";
		else
			cout << "no\n";
	}
	return 0;
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n)
      : connected_(n, vector<bool>(n, false)), done_(n, false) {}
  void register_friend(int s, int t) {
    connected_[s][t] = connected_[t][s] = true;
  }
  bool can_reach(int s, int t) {
    if (s == t)
      return true;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      if (v == t)
        return true;
      if (done_[v])
        continue;
      done_[v] = true;
      auto& con = connected_[v];
      for (size_t i = 0; i < con.size(); ++i) {
        if (con[i]) {
          auto v2 = static_cast<int>(i);
          if (v2 == t)
            return true;
          if (!done_[v2]) {
            q.push(v2);
          }
        }
      }
    }
    return false;
  }

 private:
  vector<vector<bool>> connected_;
  vector<bool> done_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}

C++: #include<iostream>
#include<vector>
 
using namespace std;
 
int main(){
    int n, m, s, t, q, i, j;
    int id, color = 0;
    bool answer_yes = false;
 
    // enter
    cin >> n >> m;
    vector< vector<int> > Adj(n);
    vector<int> Stack;
    vector<int> Check(n, 0);
    vector<int>::iterator it, itend;
    for (i = 0; i < m; i++) {
        cin >> s >> t;
        Adj[s].push_back(t);
        Adj[t].push_back(s);
    }
    /*for (i = 0; i < n; i++) {
        cout << "\n" << i << " : " << flush;
        for (auto it2 = Adj[i].begin(); it2 != Adj[i].end(); it2++) {
            cout << *it2 << " " << flush;
        }
    }
    cout << endl;*/
 
    // calculate
    for (i = 0; i < n; i++) {
        if (Check[i] == 0) {
            color++;
            Stack.push_back(i);
            Check[i] = color;
            while (!Stack.empty()) {
                id = Stack.back();
                //cout << "\nid : " << id << " color : " << color << endl;
                itend = Adj[id].end();
                for (it = Adj[id].begin(); it != itend; it++) {
                    if (!Check[*it]) {
                        Stack.push_back(*it);
                        Check[*it] = color;
                        break;
                    }
                }
                if (it == itend) {
                    Stack.pop_back();
                }
            }
        }
    }
 
    //find
    cin >> q;
    for (i = 0; i < q; i++) {
        cin >> s >> t;      
        if (Check[s] == Check[t]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
     
    return 0;
}