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>

#define inf 1000000007

using namespace std;

vector<vector<int> > connect;
vector<int> visited;
int n;

void dfs(int start,int c){
	stack<int> st;
	st.push(start);
	while(!st.empty()){
		int k = st.top();
		visited[k] = c;
		st.pop();
		for(int i=0;i<connect[k].size();++i){
			int tmp = connect[k][i];
			if(visited[tmp]==-1){
				st.push(tmp);
			}
		}
	}
}

int main(){
	int m,q;
	cin >> n >> m;
	connect.resize(n);
	visited.resize(n);
	for(int i=0;i<m;++i){
		int s,t;
		cin >> s >> t;
		connect[s].push_back(t);
		connect[t].push_back(s);
	}
	int c = 0;
	for(int i=0;i<n;++i)visited[i] = -1;
	for(int i=0;i<n;++i){
		if(visited[i] == -1){
			dfs(i,c);
			c++;
		}
	}
	cin >> q;
	vector<bool> res(q);
	for(int i=0;i<q;++i){
		int from,to;
		cin >> from >> to;
		if(visited[from] == visited[to]){
			res[i] = true;
		}else{
			res[i] = false;
		}
	}
	for(int i=0;i<q;++i){
		if(res[i])cout << "yes" << "\n";
		else cout << "no" << "\n";
	}
}

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

void bfs(int s);

int n_user,m,*deg;
vector<int> *G; 

int main(void){
	cin>>n_user>>m;
	deg = new int[n_user];
	G = new vector<int>[n_user];
	for(int i=0;i<n_user;i++)
		deg[i] = -1;

	int user1,user2;
	for(int i=0;i<m;i++){
		cin>>user1>>user2;
		G[user1].push_back(user2);
		G[user2].push_back(user1);
	}

	for(int i=0;i<n_user;i++){
		cout<<i<<" : ";
		for(int j=0;j<G[i].size();j++){
			cout<<G[i][j]<<" ";
		}
		cout<<endl;
	}

	for(int i=0;i<n_user;i++) bfs(i);

	int question,s,t;
	cin>>question;
	for(int i=0;i<question;i++){
		cin>>s>>t;
		if(deg[s]!=-1){
			if(deg[s]==deg[t])
				cout<<"yes"<<endl;
			else
				cout<<"no"<<endl;
		}
	}

	delete [] G; delete [] deg;
}

void bfs(int s){
	queue<int> q;
	q.push(s);
	if(deg[s]==-1) deg[s] = s;
	else return ;

	int u;
	while(!q.empty()){
		u = q.front();
		q.pop();
		for(int v=0;v<G[u].size();v++){
			if(deg[G[u][v]]==-1){
				deg[G[u][v]] = deg[u];
				q.push(G[u][v]);
			}
		}
	}
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
vector<int> g[100000];
vector<int> c(100000);

void dfs(int v, int col) {
    c[v] = col;
    for (int i = 0; i < g[v].size(); ++i) {
        if(c[g[v][i]] == 0) dfs(g[v][i], col);
    }
}

int main() {
    int n, m, q;
    cin >> n>> m;
    for (int i = 0; i < m; ++i) {
        int v, w;
        cin >> v >> w;
        g[w].push_back(v);
        g[v].push_back(w);
    }

    for (int k = 0; k < n; ++k) {
        c[k] = 0;
    }

    int col = 1;
    for (int j = 0; j < n; ++j) {
        if(!c[j]) {
            dfs(j, col);
            col++;
        }
    }

    cin >> q;
    for (int l = 0; l < q; ++l) {
        int v,w;
        cin >> v >> w;
        if(c[v] == c[w]) cout << "yes";
        else cout << "no";
        cout << endl;

    }


    return 0;
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

const int SIZE = 100001;
int group[SIZE];

void assignGroup(int u, int g, std::vector<int> *a) {
  if (group[u] != 0) return;

  group[u] = g;
  // for (int i = 0; i < a[u].size(); ++i) {
  //   if (a[u][i] && group[i] == 0) assignGroup(i, g, a);
  // }
  for (auto v : a[u]) {
    // printf("u: %d, v: %d, group[u]: %d, group[v]: %d\n", u, v, group[u],
    //        group[v]);
    if (group[v] == 0) assignGroup(v, g, a);
  }
}

bool isConnected(int s, int t) { return group[s] == group[t]; }

int main() {
  int n, m;
  std::cin >> n >> m;
  std::vector<int> a[n];

  rep(i, m) {
    int s, t;
    std::cin >> s >> t;
    a[s].push_back(t);
    a[t].push_back(s);
  }

  // std::cout << "/* message */" << '\n';
  // rep(i, n) {
  //   for (auto v : a[i]) std::cout << v << ' ';
  //   std::cout << '\n';
  // }
  // std::cout << "/* message */" << '\n';

  int g = 0;
  rep(i, n) {
    if (group[i] == 0) { assignGroup(i, ++g, a); }
  }

  // rep(i, n) std::cout << group[i] << ' ';

  int q;
  std::cin >> q;
  rep(i, q) {
    int s, t;
    std::cin >> s >> t;
    if (isConnected(s, t))
      std::cout << "yes" << '\n';
    else
      std::cout << "no" << '\n';
  }

  return 0;
}