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++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_11_D: Connected Components

#include <algorithm>
#include <cstdio>
#include <stack>
#include <vector>

using uint = unsigned int;

class Graph {
  std::vector<std::vector<uint>> g_;
  std::vector<int> root_;
  enum { NIL = -1, IN_STACK= -2 };

public:
  Graph(uint n)
  {
    g_.resize(n);
    root_.resize(n);
  }

  void add(uint from, uint to)
  {
    g_[from].push_back(to);
    g_[to].push_back(from);
  }

  void setup()
  {
    std::fill(root_.begin(), root_.end(), NIL);
    for (auto i = 0u; i < g_.size(); ++i ) {
      if (root_[i] == NIL) {
        dfs(i);
      }
    }
  }

  bool reachable(uint from, uint to) const
  {
    return root_[from] == root_[to];
  }

private:
  void dfs(uint root)
  {
    std::stack<uint> s;
    s.push(root);
    while (!s.empty()) {
      auto i = s.top();
      s.pop();
      root_[i] = root;
      for (auto j : g_[i]) {
        if (root_[j] == NIL) {
          root_[j] = IN_STACK;
          s.push(j);
        }
      }
    }
  }
};

int main()
{
  uint n, m;
  std::scanf("%u %u", &n, &m);

  Graph g(n);

  uint s, t;
  for (auto i = 0u; i < m; ++i) {
    std::scanf("%u %u", &s, &t);
    g.add(s, t);
  }
  g.setup();

  uint q;
  std::scanf("%u", &q);
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%u %u", &s, &t);
    std::puts(g.reachable(s, t) ? "yes" : "no");
  }
}

// eof

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

int d[MAX];
vector<int> V[MAX];
int tt=0,n,m;
queue<int> Q;

void gogo(int x){
  Q.push(x);
  d[x]=tt;
  int v;

  while(!Q.empty()){
    v=Q.front(); Q.pop();
    for(int i=0;i<V[v].size();i++){
      if(d[V[v][i]]==-1) {
        Q.push(V[v][i]);
        d[V[v][i]]=d[v];

      }
    }
  }

}

void gostart(){
  for(int i=0;i<n;i++){
    if(d[i]==-1) {
      gogo(i);
      tt++;
    }
  }
}

int main()  {
  cin>>n>>m;
  for(int i=0;i<n;i++){
    d[i]=-1;
  }

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

  gostart();

  int q;
  cin>>q;
  int k,l;
  for(int i=0;i<q;i++){
    cin>>k>>l;
    if(d[k]==d[l]) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>

class Graph{
public:
	std::vector< std::vector<int> > A;
	std::vector<int> Cluster;
	int n;

	Graph(int n);

	bool isConnected(int s, int t);

	void MakeCluster();

	void Link(int s, int t);

};

Graph::Graph(int n){
	A.resize(n);
	Cluster.resize(n);
	for (int i = 0; i < n; i++) Cluster[i] = 0;
	this->n = n;
}

void Graph::Link(int s, int t){
	A[s].push_back(t);
	A[t].push_back(s);
	return;
}

void Graph::MakeCluster(){
	int clusternum = 0;
	std::list<int> Q;
	for (int i = 0; i < n; i++){
		if (Cluster[i] == 0){
			Q.clear();
			clusternum++;
			Cluster[i] = clusternum;
			Q.push_back(i);
			bool res = false;
			while (!Q.empty()){
				int f = Q.front();
				Q.pop_front();
				for (std::vector<int>::iterator itr = A[f].begin(); itr != A[f].end(); itr++){
					if (Cluster[*itr] == false){
						Cluster[*itr] = clusternum;
						Q.push_back(*itr);
					}
				}
			}
		}
	}
	return;
}

bool Graph::isConnected(int s, int t){
	return Cluster[s] == Cluster[t];
}

int main(){
	int n, m, s, t, q;
	scanf("%d%d", &n, &m);
	Graph G(n);
	for (int i = 0; i < m; i++){
		scanf("%d%d", &s, &t);
		G.Link(s, t);
	}
	scanf("%d", &q);
	G.MakeCluster();
	for (int i = 0; i < q; i++){
		scanf("%d%d", &s, &t);
		if (G.isConnected(s, t)) printf("yes\n"); else printf("no\n");
	}
	
	return 0;
}

C++: #include <iostream>
using namespace std;

int a[100000];
int search(int k){
  if(a[k]==k){
      return k;
  }
  int p=search(a[k]);
  a[k]=p;
  return p;
}

int main(){
  for(int i=0;i<100000;i++){
    a[i]=i;
  }
  int n,m,p,q,r,s,t;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>p>>q;
    a[search(p)]=search(q);
  }
  cin>>r;
  for(int i=0;i<r;i++){
    cin>>s>>t;
    if(search(s)==search(t)){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
  }
  return 0;
}