Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	m := getNextInt(scanner)
	nodes := make([]Node, n)

	for i := 0; i < n; i++ {
		nodes[i].list = make([]int, 0)
	}

	for i := 0; i < m; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		nodes[s].list = append(nodes[s].list, t)
		nodes[t].list = append(nodes[t].list, s)
	}

	c := 1
	for i := 0; i < n; i++ {
		if nodes[i].c == 0 {
			traverse(nodes, i, c)
			c++
		}
	}

	q := getNextInt(scanner)

	for i := 0; i < q; i++ {
		s := getNextInt(scanner)
		t := getNextInt(scanner)
		if nodes[s].c == nodes[t].c {
			fmt.Fprintln(writer, "yes")
			continue
		}
		fmt.Fprintln(writer, "no")
	}

	writer.Flush()
}

// Node ...
type Node struct {
	list []int
	c    int
}

func traverse(nodes []Node, r, c int) {
	nodes[r].c = c
	for _, i := range nodes[r].list {
		if nodes[i].c != 0 {
			continue
		}
		traverse(nodes, i, c)
	}
}

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;
}