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

using namespace std;

int main() {
  vector<int> R[100000];
  queue<int> Q;
  int n,m,s,t,q;
  int visit[100000];

  cin >> n >> m;
  for(int i=0;i<m;i++){
    cin >> s >> t;
    R[s].push_back(t);
    R[t].push_back(s);
  }
  cin >> q;
  for(int i=0;i<q;i++){
    cin >> s >> t;
    for(int j=0;j<n;j++) visit[j] = 0;
    Q.push(s);
    int f=0;
    while(Q.empty() == false){
      int u = Q.front();
      Q.pop();
      visit[u] = 1;
      if(u==t){
	f = 1;
      }
      for(int j=0;j<R[u].size();j++){
	int v = R[u][j];
	if(visit[v]==0) Q.push(v);
      }
    }
    if(f==1) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#define MAX 100000000
#define NIL -1

int G[MAX][MAX];
int n, color[MAX], N[MAX], top=0;


void push(int x) {
  N[++top]=x;
}

int pop() {
  top--;
  return N[top+1];
}

int isEmpty() {
  return top==0;
}

void dfs(int r, int c) {
  int i, v, u;
  push(r);
  color[r]=c;
  while (! isEmpty()) {
    u=N[top];
    pop();
    for (i=0; i<n; i++) {
      if (G[u][i]==0) continue;
        v=i;
      if (color[v]==NIL) {
        color[v]=c;
        push(v);
      }
    }
  }
}



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

int main() {

  int  m, i, j, v, s, t, q;

  scanf("%d %d", &n, &m);

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

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

  scanf("%d", &q);

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

  return 0;

}

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

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int n, m;
struct node {
    int parent;
    vector<int> child;
};

int main() {
    cin >> n >> m;
    node person[n];
    REP(i, n)
        person[i].parent = -1;
    REP(i, m){
        int s, t;
        cin >> s >> t;
        if(person[s].parent == -1 && person[t].parent == -1){
            person[s].parent = s;
            person[t].parent = s;
            person[s].child.push_back(t);
        } else if(person[s].parent == -1){
            person[s].parent = person[t].parent;
            person[t].child.push_back(s);
        } else if(person[t].parent == -1){
            person[t].parent = person[s].parent;
            person[s].child.push_back(t);
        } else {
            person[t].parent = person[s].parent;
            REP(j, person[t].child.size()){
                person[person[t].child[j]].parent = person[s].parent;
                person[person[s].parent].child.push_back(person[t].child[j]);
            }
        }
    }
    int q;
    cin >> q;
    REP(i, q){
        int s, t;
        cin >> s >> t;
        if(person[s].parent != -1 && person[t].parent != -1  && person[s].parent == person[t].parent)
            cout << "yes" << endl;
        else
            cout << "no" << endl;
    }
    return 0;
}

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

using namespace std;

const int N = 100000;
int n;
// int color[N];
int group[N];
vector<int> v[N];

void setColor(int r, int c){
  queue<int> q;
  q.push(r);
  group[r] = c;

  while(!q.empty()){
    int x = q.front(); q.pop();
    for(int i=0;i<v[x].size();i++){
      int y = v[x][i];
      if(group[y] == -1){
        q.push(y);
        group[y] = c;
      }
    }
  }
}

void assignColor(){
  int id = 1;
  for(int i=0;i<n;i++) group[i] = -1;
  for(int u=0;u<n;u++){
    if(group[u] == -1) setColor(u, id++);
  }
}

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

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