Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

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 )
    j=d[j];
}
while(d[h]!=h )
    h=d[h];
}
if(j!=h){
    d[j]=h;
    d[a]=h;
    d[b]=h;
}
}
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 )
    j=d[j];
}
while(d[h]!=h )
    h=d[h];
}
    if(j==h){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
}
    return 0;
}

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

vector<int> ad[100000];
vector<int> v,d;

int main()
{
	int i,j,n,m,a,b,q,l,k;
	queue<int> Q;
	
	cin >> n;
	cin >> m;
	for(i=0;i<=n-1;i++) ad[i].push_back(-1);
	for(i=0;i<=m-1;i++)
	{
		cin >> a;
		cin >> b;
		ad[a].push_back(b);
		ad[b].push_back(a);
	}
	cin >> q;
	for(i=0;i<=q-1;i++)
	{
		cin >> a;
		cin >> b;
		v.clear();
		d.clear();
		for(j=0;j<=n-1;j++)
		{
			v.push_back(0);
			d.push_back(-1);
		}
		v[a]=1;
		d[a]=0;
		Q.push(a);
		while(Q.empty()!=1)
		{
			l=Q.front();
			for(k=0;ad[l][k]!=-1;k++)
			{
				if(v[ad[l][k]]==0)
				{
				    Q.push(ad[l][k]);
				    v[ad[l][k]]=1;
					d[ad[l][k]]=d[l]+1;
				}
			}
			Q.pop();
		}
		if(d[b]>=0) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: //Connected Component
#include <iostream>
#include <vector>

using namespace std;

static const int N = 100000;
int n, colors[N] = {};
vector<int> G[N];

void dfs_visit(int u, int c) {
    if(colors[u] == c) return;
    colors[u] = c;
    for(int i = 0; i < G[u].size(); i++) {
        dfs_visit(G[u][i], c);
    }
}

void dfs() {
    for(int i = 0; i < N; i++) colors[i] = -1;
    for(int i = 0; i < n; i++) {
        if(colors[i] == -1) {
            dfs_visit(i, i);
        }
    }
}

int main() {
    int m, s, t, q;
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    
    dfs();
    
    cin >> q;
    for(int i = 0; i < q; i++) {
        cin >> s >> t;
        if(colors[s] == colors[t]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
}

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

vector<int> neighbors[100005];
int root[100005];

void dfs(int i, int start) {
  root[i] = start;
  // visit all i neighbors
  for (int j : neighbors[i]) {
    if (root[j] == -1) {
      dfs(j, start);
    }
  }
}

int main() {
  int n, m, x, y, q;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d %d", &x, &y);
    neighbors[x].push_back(y);
    neighbors[y].push_back(x);
  }
  memset(root, -1, sizeof(root));
  for (int i = 0; i < n; i++) {
    if (root[i] == -1)
      dfs(i, i);
  }
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &x, &y);
    if (root[x] == root[y])
      printf("yes\n");
    else
      printf("no\n");
  }
}

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

#define null -1

void DFS(vector<int> rVal[], map<int, int> &uColor_m, int idOrSs) {
	stack<int> stack;
	stack.push(idOrSs);
	uColor_m[idOrSs] = idOrSs;

	while (!stack.empty()) {
		int curUVal = stack.top();
		stack.pop();
		for (int i = 0; i < (signed)rVal[curUVal].size(); i++) {
			int curV = rVal[curUVal][i];
			if (uColor_m[curV] != null) continue;
			uColor_m[curV] = idOrSs;
			stack.push(curV);
		}
	}
}

void assignColor(int uNum, vector<int> rVal[], map<int, int> &uColor_m) {
	for (int i = 0; i < uNum; i++)
		uColor_m[i] = null;
	for (int i = 0; i < uNum; i++) {
		if (uColor_m[i] == null) DFS(rVal, uColor_m, i);
	}
}

int main() {
	int userNum;
	int relationNum;
//	int uColor_map[100000]; //<?????????, ??°??????????????????>
//	map<int, int> uColor_m;
	map<int, int> uColor_m;
	//??????????????¨????????????
	vector<int> relationVal[100000];
	//int userColor[100000];

	cin >> userNum >> relationNum;
	for (int i = 0; i < relationNum; i++) {
		int userVal1, userVal2;
		cin >> userVal1 >> userVal2;
		relationVal[userVal1].push_back(userVal2);
		relationVal[userVal2].push_back(userVal1);
		uColor_m[i] = null;
	}

	assignColor(userNum, relationVal, uColor_m);

	int qNum;
	cin >> qNum;
	for (int i = 0; i < qNum; i++) {
		int uV1, uV2;
		cin >> uV1 >> uV2;
		if (uColor_m[uV1] == uColor_m[uV2])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}