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 <list> 
#define N 100001
using namespace std; 
int n, loop;
int color=0;

int flag[N];

bool *visited = new bool[N];

class Graph{ 
    int V; 
    list<int> *adj; 
  
public: 
    void DFSUtil(int v); 
    Graph(int V);  
    void addEdge(int v, int w); 
    void connectedComponents(); 
}; 
  
void Graph::connectedComponents() { 
    for(int v = 0; v < V; v++){
        visited[v] = false; 
	}
  
    for (int v=0; v<V; v++) { 
        if (visited[v] == false) {
        	color++;
            DFSUtil(v); 
        } 
    } 
}
  
void Graph::DFSUtil(int v) 
{ 
    visited[v] = true;
    flag[v]=color;
    list<int>::iterator i; 
    for(i = adj[v].begin(); i != adj[v].end(); ++i){
        if(!visited[*i]){
            DFSUtil(*i); 
		}
	}
} 
  
Graph::Graph(int V) { 
    this->V = V; 
    adj = new list<int>[V]; 
} 
  
void Graph::addEdge(int v, int w) { 
    adj[v].push_back(w); 
    adj[w].push_back(v); 
} 

int main() {
  	int i, x,y,uji,j;
  	cin>>n>>loop;
	Graph g(n);
	 
	
	
  	for(i=0; i<loop; i++){
		cin>>x>>y;
		g.addEdge(x,y);
	}
	
	for(j= 0; j < n; j++){
	   visited[j] = false; 
	   flag[j]=0;
	}
	g.connectedComponents();
	
	cin>>uji;
	int prev=-1;
	for(i=0; i<uji; i++){
		cin>>x>>y;
		
		if(flag[y]==flag[x]){
			cout<<"yes"<<endl;
		}else{
			cout<<"no"<<endl;
		}
		prev=x;
	}
    return 0; 
}

C++: #include <bits/stdc++.h>
using namespace std;
// #define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
#define ITR(v,c) for(auto v=begin(c);v!=end(c);v++)
#define FORE(x,c) for(auto &x:c)
#define FOR(v,a,n) for(int v=a;v<(int)(n);v++)
#define REP(v,n) FOR(v,0,n)
#define RREP(v,n) for(int v=((int)(n)-1);v>=0;v--)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define SZ(c) ((int)c.size())
const int DX[9]={0,1,0,-1,1,1,-1,-1,0}, DY[9]={-1,0,1,0,-1,1,1,-1,0};
const int INF=1e9; const long long INFLL=1e18;
template<class T> ostream& operator << (ostream &os, const vector<T> &v) {
    ITR(i,v) os<<*i<<(i==end(v)-1?"":" "); return os; }
template<class T> istream& operator >> (istream &is, vector<T> &v) {
    ITR(i,v) is>>*i; return is; }
//------------------------------------------------------------------------------

//-------- Union Find
struct UnionFind {
	vector<int> data;
	UnionFind(int size) : data(size, -1) { }
    bool unionSet(int x, int y) {
	    x = root(x); y = root(y);
        if(x != y) {
            if(data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x;
        }
        return x != y;
	}
	bool findSet(int x, int y) {
	    return root(x) == root(y);
    }
	int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

signed main() {
    int n,m;
    cin>>n>>m;
    UnionFind uf(n);
    REP(i,m) {
        int a,b;
        cin>>a>>b;
        a--,b--;
        uf.unionSet(a,b);
    }

    int q;
    cin>>q;
    REP(i,q) {
        int a,b;
        cin>>a>>b;
        a--,b--;
        if(uf.root(a)==uf.root(b)) {
            cout<<"yes"<<endl;
        }
        else {
            cout<<"no"<<endl;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <stack>
#include <vector>

using namespace std;
const int nil = -1;

int total, n, m, q;

vector<int>* vecs;
typedef int Comm;
Comm* comms;

void find_connection(int from){
	stack<int> stk;
	stk.push(from);

	while(stk.size()){
		int top = stk.top();
		comms[top] = from;
		for(int i=0, sz=vecs[top].size(); i<=sz; ++i){
			if(i==sz){
				stk.pop();
				break;
			}
			if(comms[vecs[top][i]] == nil){
				stk.push(vecs[top][i]);
				break;
			}
		}
	}
}

int main()
{
	scanf("%d %d",&n,&m);
	vecs = new vector<int> [n];
	comms = new Comm [n];

	for(int i=0; i<n; ++i){
		comms[i] = nil;
	}

	for(int i=0; i<m; ++i){
		int id, f;
		scanf("%d %d",&id,&f);
		vecs[id].push_back(f);
		vecs[f].push_back(id);
	}

	scanf("%d",&q);
	for(int i=0; i<q;++i){
		int from, to;
		scanf("%d %d",&from,&to);
		if(comms[from]==nil && comms[to]==nil){
			find_connection(from);
			if(comms[from] == comms[to]){
				cout << "yes" << endl;
			}
			else{
				cout << "no" << endl;
			}
		}
		else if(comms[from] == comms[to]){
			cout << "yes" << endl;
		}
		else{
			cout << "no" << endl;
		}
	}

	delete[] comms;
	delete[] vecs;

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 100000+5
int color[MAX];
vector<int> u[MAX];
int num;
void ranse(int p,int color_num){
	stack<int> s;
	int a;
	s.push(p);color[p]=color_num;
	while(!s.empty()){
		a=s.top();s.pop();
		for(int i=0;i<u[a].size();i++){
		if(color[u[a][i]]==-1){
			s.push(u[a][i]);color[u[a][i]]=color_num;
		}
	}
	}
}
int main(){
	int m;
	memset(color,-1,sizeof(color));
	cin>>num>>m;
	for(int i=0;i<m;i++){
		int a,b;
		cin>>a>>b;
		u[a].push_back(b);u[b].push_back(a);
	}
	int color_num=0;
	for(int i=0;i<num;i++){
		if(color[i]==-1){
			ranse(i,color_num);color_num++;
		}
	}
	int order_num;
	cin>>order_num;
	for(int i=0;i<order_num;i++){
		int a,b;
		cin>>a>>b;
		if(color[a]==color[b]) cout<<"yes"<<endl;
		else cout<<"no"<<endl;
	}
}