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 <vector>
#include <stack>

using namespace std;
using Relations = vector<vector<int>>;

const int no_group = -1;
const int no_user_id = -1;

void depth_first_search(Relations& list_graph, int started_user_id, vector<int>& connected_group, int group_id) {
  stack<int> visited_path;
  visited_path.push(started_user_id);
  connected_group[started_user_id] = group_id;

  while(!visited_path.empty()) {
    int current_user_id = visited_path.top();

    int unvisited_adjacency_user_id = no_user_id;
    for(int i = 0; i < list_graph[current_user_id].size(); i++) {
      int adjacency_user_id = list_graph[current_user_id][i];
      if(connected_group[adjacency_user_id] == no_group) {
        unvisited_adjacency_user_id = adjacency_user_id;
        break;
      }
    }

    if(unvisited_adjacency_user_id != no_user_id) {
      connected_group[unvisited_adjacency_user_id] = group_id;
      visited_path.push(unvisited_adjacency_user_id);
    } else {
      visited_path.pop();
    }
  }
}


int main() {
  int user_num;
  int relation_num;
  Relations list_graph;

  // Init
  cin >> user_num >> relation_num;
  list_graph.resize(user_num);

  // Construct list graph
  for(int i = 0; i < relation_num; i++) {
    int user1_id, user2_id;
    cin >> user1_id >> user2_id;
    list_graph[user1_id].push_back(user2_id);
    list_graph[user2_id].push_back(user1_id);
  }

  // Group connected component
  vector<int> connected_group(user_num, no_group);

  int group_id = 0;
  for(int i =  0; i < user_num; i++) {
    if(connected_group[i] == no_group) {
      depth_first_search(list_graph, i, connected_group, group_id++);
    }
  }


  // Answer queries
  int query_num;
  cin >> query_num;
  for(int i = 0; i < query_num; i++) {
    int user1_id, user2_id;
    cin >> user1_id >> user2_id;

    if(connected_group[user1_id] == connected_group[user2_id]) {
      cout << "yes";
    } else {
      cout << "no";
    }
    cout << endl;
  }
}

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

using namespace std;

static const int MAX = 100000;

int n, color[MAX];
vector<int> G[MAX];

void dfs (int r, int c) {

    int i, u, v;

    stack<int> S;
    S.push(r);
    color[r] = c;

    while (!S.empty()) {
        u = S.top();
        S.pop();
        for (i = 0; i < G[u].size(); i++) {
            v = G[u][i];
            if (color[v] == -1) {
                color[v] = c;
                S.push(v);
            }
        }
    }

}

void assignColor () {

    int id = 1, i, u;

    for (i = 0; i < n; i++) {
        color[i] = -1;
    }
    for (u = 0; u , n; u++) {
        if (color[u] = -1) {
            dfs (u, id++);
        }
    }

}

int main () { 

    int s, t, m, q, i;

    cin >> n >> m;

    for (i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assignColor ();

    cin >> q;

    for (i = 0; i < q; i++) {
        cin >> s >> t;
        if (color[s] == color[t]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;

}

C++: #include<cstdio>
#include<queue>

using namespace std;

static const int N = 100000;

int m,n;

queue<int> Q[N];
int color[N];

void visit(int a,int c){
    color[a] = c;
   while( !(Q[a].size() == 0) ){
        int u = Q[a].front();
        if(color[u] == 0) visit(u,c);
        Q[a].pop();
    }
}

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

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

    for(int i=0; i<m;i++){
        int u,v;
        scanf("%d",&u);
        scanf("%d",&v);
        Q[u].push(v);
        Q[v].push(u);
    }
    
    int cc = 1;

    for(int i=0; i<n; i++){
        if(color[i] == 0){
            visit(i,cc);
            cc++;
        }
    }

    int l; 
    scanf("%d",&l);
    for(int i =0 ;i<l;i++){
        int x,y;
        scanf("%d",&x);
        scanf("%d",&y);

        if(color[x] == color[y]) printf("yes\n");
        else printf("no\n");

    }

}