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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define REPR(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

using Graph = vector<vector<int> >;

vector<int> seen;

void dfs(const Graph &G, int v, int c){
  seen[v] = c;

  for(int nv : G[v]){
    if(seen[nv] != -1) continue;
    dfs(G, nv, c);
  }
}

int main(){
  int n, m; cin >> n >> m;

  Graph G(n);
  rep(i, m){
    int a, b; cin >> a >> b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  seen.assign(n, -1);
  int c = 0;
  rep(i, n){
    if(seen[i] == -1){
      dfs(G, i, c);
      c++;
    }
  }

  int q; cin >> q;
  rep(i, q){
    int s, t; cin >> s >> t;
    cout << (seen[s] == seen[t] ? "yes" : "no") << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
	int n, m;
	cin >> n >> m;
	vector<vector<int>> g( n );
	for( int i = 0; i < m; i++ ) {
		int s, t;
		cin >> s >> t;
		g[s].push_back( t );
		g[t].push_back( s );
	}
	vector<int> color( n );
	int col = 0;
	for( int i = 0; i < n; i++ ) {
		if( color[i] > 0 ) continue;
		col++;
		color[i] = col;
		queue<int> que;
		que.push( i );
		while( !que.empty() ) {
			int u = que.front();
			que.pop();
			for( auto v : g[u] ) {
				if( color[v] > 0 ) continue;
				que.push( v );
				color[v] = col;
			}
		}
	}

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

C++: #include<iostream>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;

typedef pair<int, int> P;

void dfs(int s, int group, int n, vector<vector<int>> v, vector<int> &used) {
	used[s] = group;
	int k = v[s].size();
	REP(l, k) {
		int j = v[s][l];
		if (!used[j]) {
			dfs(j, group, n, v, used);
		}
	}
}

void divide(int n, vector<vector<int>> v, vector<int> &used) {
	int s = 0;
	int group = 1;
	while (s < n) {
		dfs(s, group++, n, v, used);
		while (s < n && used[s]) s++;
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;
	vector<vector<int>> v(n);
	REP(i, m) {
		int s, t;
		cin >> s >> t;
		v[s].push_back(t);
		v[t].push_back(s);
	}
	vector<int> used(n, 0);
	divide(n, v, used);
	int q;
	cin >> q;
	REP(i, q) {
		int s, t;
		cin >> s >> t;
		if (used[s] == used[t]) cout << "yes\n";
		else cout << "no\n";
	}
	

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

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

void dfs(int r, int c){
    stack<int> S;
    S.push(r);
    color[r] = c;
    while(!S.empty()){
        int u = S.top(); S.pop();
        for(int i = 0; i < G[u].size(); i++){
            int v = G[u][i];
            if(color[v] == NIL){
                color[v] = c;
                S.push(v);
            }
        }
    }
}

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

int main(){
    int s, t, m, q;

    cin >> n >> m;

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

    cin >> q;

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

    return 0;
}

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

const int MAX = 100000;
vector<int> friends[MAX + 1];
int groups[MAX + 1];

void dfs(int id, int gid) {
  if (groups[id] != -1) return;
  groups[id] = gid;

  int l = friends[id].size();
  for (int i = 0; i < l; i++) {
    dfs(friends[id][i], gid);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;

  int a, b, q;
  int gid = 0;

  for (int i = 0; i < m; i++) {
    cin >> a >> b;
    friends[a].push_back(b);
    friends[b].push_back(a);
  }

  for (int i = 0; i < n; i++) groups[i] = -1;

  for (int i = 0; i < n; i++) {
    if (groups[i] == -1) dfs(i, gid++);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    if (groups[a] == groups[b]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << ":" << groups[i] << endl;
  }
  return 0;
}