Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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