Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

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 <bits/stdc++.h>
using namespace std;

struct Graph {
    vector<vector<int>> adj;
    vector<int> color;
    int colorID;
    Graph(int n) {
        adj.assign(n, vector<int>());
        color.assign(n, -1);
        colorID = 0;
    }
};

void dfs(Graph& g, int u, int colorID)
{
    g.color[u] = colorID;
    for (auto v : g.adj[u]) {
        if (g.color[v] == -1) {
            g.color[v] = colorID;
            dfs(g, v, colorID);
        }
    }
}

int main()
{
    int n, k;
    cin >> n >> k;
    Graph g(n);
    for (int i = 0; i < k; i++) {
        int u, v;
        cin >> u >> v;
        g.adj[u].push_back(v);
        g.adj[v].push_back(u);
    }

    for (int u = 0; u < n; u++) {
        if (g.color[u] == -1) {
            dfs(g, u, g.colorID++);
        }
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int u, v;
        cin >> u >> v;
        if (g.color[u] == g.color[v]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
}

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

using ll = long long;
using namespace std;

/* initialize */
vector<int> A[100000];
int c[100000];
/* main */
void dfs(int r, int d){
    stack<int> S;
    S.push(r);
    c[r] = d;
    while (!S.empty()){
        int u = S.top(); S.pop();
        for (int v : A[u]) {
            if ( c[v] == -1){
                c[v] = d;
                S.push(v);
            }
        }
    }
}
int main() {
    int n, m, k, l;
    cin >> n;
    cin >> m;
    for (int i = 0; i < m; ++i) {
        scanf("%d %d", &k, &l);
        A[k].push_back(l);
        A[l].push_back(k);
    }
    int id = 1;
    for (int i = 0; i < n; ++i) {
        c[i] = -1;
    }
    for (int j = 0; j < n; ++j) {
        if (c[j] == -1) dfs(j,id++);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; ++i) {
        cin >> k >> l;
        if(c[k] == c[l]) cout << "yes\n";
        else cout << "no\n";
    }
    return 0;
}

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

vector<int> G[100000], color(100000);
int c;
void dfs(int v) {
    color[v] = c;
    for (int i = 0; i < G[v].size(); ++i) {
        int e = G[v][i];
        if (!color[e]) dfs(e);
    }
}
int main() {
    int n, m;
    cin >> n >> m;
    while (m--) {
        int s, t;
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    for (int i = 0; i < n; ++i) {
        if (!color[i]) {
            c++;
            dfs(i);
        }
    }
    int q;
    cin >> q;
    while (q--) {
        int s, t;
        cin >> s >> t;
        if (color[s] == color[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

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

typedef struct{
	std::vector<int> neighbors;
	int cluster;
} node;

void dfs(node* nodes, int id, int cluster){
	for (int i=0; i<nodes[id].neighbors.size(); i++){
		if (nodes[nodes[id].neighbors[i]].cluster == -1){
			nodes[nodes[id].neighbors[i]].cluster = cluster;
			dfs(nodes, nodes[id].neighbors[i], cluster);
		}
	}
}

int main(){
	int n, d, num_relation, n_query, s, t, cluster;
	node *nodes;

	scanf("%d %d", &n, &num_relation);
	nodes = new node[n];
	for (int i=0; i<n;i++){
		nodes[i].cluster = -1;
	}
	for(int i=0;i<num_relation;i++){
		scanf("%d %d", &s, &t);
		nodes[s].neighbors.push_back(t);
		nodes[t].neighbors.push_back(s);
	}
	cluster = -1;
	for (int i=0;i<n;i++){
		cluster ++;
		if (nodes[i].cluster == -1) dfs(nodes, i, cluster);
	}
	scanf("%d", &n_query);
	for(int i=0;i<n_query;i++){
		scanf("%d %d", &s, &t);
		if (nodes[s].cluster != -1 && nodes[s].cluster != -1 && nodes[s].cluster == nodes[t].cluster) printf("yes\n");
		else printf("no\n");

	}

	delete []nodes;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<stack>
#include<string>
using namespace std;

vector< vector<int> > L(100005);
int c[100005]={};
bool r;

void dfs (int v, int marker){
	if(c[v]!=0){
		r=false;
		return;
	}
	r=true;
	c[v]=marker;
	for (int i=0;i<L[v].size();i++){
		dfs(L[v][i],marker);
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n,m,a,b,q,s,t;
	cin >> n >> m;
	
	for (int i=0;i<m;i++){
		cin >> a >> b;
		L[a].push_back(b);
	}
	
	int marker=1;
	for (int i=0;i<n;i++){
		dfs(i,marker);
		if (r) marker++;
	}
	
	
	for (int i=0;i<n;i++){
		cout << c[i] <<endl;
	}
	
	cin >> q;
	for (int i=0;i<q;i++){
		cin >> s >> t;
		if(c[s]==c[t]){
			cout << "yes" <<endl;
		}else{
			cout << "no" <<endl;
		}
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
 
int main() {
int d[100100];
int m,n,q;
cin>>n>>m;
for(int i=0;i<n;i++){
    d[i]=i;
}
for(int l=0;l<m;l++){
    int a,b;
    cin>>a>>b;
    int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
d[j]=h;
while(a!=h){
 
    d[a]=h;
    d[b]=h;
a=d[a];
b=d[b]:
}
}

cin>>q;
for(int y=0;y<q;y++){
    int a,b;
    cin>>a>>b;
        int j ,h;
    j=a,h=b;
while(d[j]!=j ||d[h]!=h){
    j=d[j];
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}