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>
#inlcude<stack>
using namespace std;
#define MAX 100000
#define NIL -1

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


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



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

int main() {

  int  m, i, j, v, s, t, q;

  cin >> n >> m;

  for (i=0; i<n; i++)
    for (j=0; j<n; j++)
      G[i][j]=0;

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

  assignColor();

  cin >> q;

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

  return 0;

}

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

int par[100001], ran[100001];
int find(int x){
	if(par[x] == x)return x;
	else return par[x] = find(par[x]);
}

void unite(int x, int y){
	x = find(x);
	y = find(y);
	if(x == y)return ;
	if(ran[x] < ran[y])par[x] = y;
	else{
		par[y] = x;
		if(ran[x] == ran[y])ran[y]++;
	}
}

bool same(int x, int y){
	return find(x) == find(y);
}

int main(){
	int n, m, q, x, y;
	scanf("%d%d", &n, &m);
	for(int i = 0;i < n;i++)par[i] = i;
	for(int i = 0;i < m;i++){
		scanf("%d%d", &x, &y);
		unite(x, y);
	}
	scanf("%d", &q);
	for(int i = 0;i < q;i++){
		scanf("%d%d", &x, &y);
		if(same(x, y))printf("yes\n");
		else printf("no\n");
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;

int find(int i, vector<int>& ds) {
    if (ds[i] == i) return i;
    ds[i] = ds[ds[i]];
    return find(ds[i], ds);
}

bool unionSets(int i, int j, vector<int>& ds) {
    int p1 = find(i, ds);
    int p2 = find(j, ds);
    if (p1 == p2) return false;

    ds[p1] = p2;
    return true;
}

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

    int n, m;
    cin>>n>>m;
    vector<int> ds(n);
    rep(i, n) ds[i] = i;
    rep(i, m) {
        int v1, v2;
        cin>>v1>>v2;
        unionSets(v1, v2, ds);
    }

    int q;
    cin>>q;
    rep(i, q) {
        int s, t;
        cin>>s>>t;
        if (find(s, ds) == find(t, ds)) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define Lens 100005

using namespace std;
int n, m, q;
vector<int> A[Lens];
queue<int>	Q;

void Find(int s, int t)
{
	Q.push(s);
	while (!Q.empty())
	{
		s = Q.front();
		Q.pop();
		for (int j = 0; j < A[s].size(); j++)
		{
			Q.push(A[s][j]);
			if (A[s][j] == t)
			{
				cout << "yes" << endl;
				return;
			}
		}
	}
	cout << "no" << endl;
	return;
}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < m; i++)
	{
		int s, t;
		cin >> s >> t;
		A[s].push_back(t);
	}
	cin >> q;
	for (int i = 0; i < q; i++)
	{
		int flag = 0;
		int s, t;
		cin >> s >> t;
		Find(s, t);
	}
	return 0;
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using ll = long long;
using VL=vector<ll>;

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
const long long INF =  999999999;
int n,m,q,d[100010],used[100100];
vector <int> g[100010];
vector <string> str;

//numは連結成分の番号
void dfs(int v,int num){
  used[v]=true;
  d[v]=num;
  REP(i,g[v].size()){
    if(!used[g[v][i]])dfs(g[v][i],num);
  }

}



int main(){
  cin >>n>>m;
  REP(i,m){
    int s,t;
    cin >>s>>t;
    g[s].push_back(t);
    g[t].push_back(s);
  }
  REP(i,n)used[i]=false;

  REP(i,n){
    if(!used[i])dfs(i,i);
  }
  cin >>q;
  REP(i,q){
    int s,t;
    cin >>s>>t;
    if(d[s]==d[t])str.push_back("yes");
    else str.push_back("no");
  }
  REP(i,str.size()){
    cout <<str[i]<<endl;
  }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef float fl;
typedef long long ll;

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();
       rep(i,G[u].size()){
           int v = G[u][i];
           if( color[v] == NIL ){
                color[v] = c;
                s.push(v);
           }
       }
    }
}

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

int main(){
    int s,t,m,q; 
    cin >> n >> m;
    rep(i,m){
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }

    assignColor();

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