Go: package main

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

const (
	MAX = 100000
	NIL = -1
)

var (
	n     int
	G     [MAX][]int
	color [MAX]int
)

func DFS(r int, c int) {
	S := list.New()
	S.PushFront(r)
	color[r] = c
	for S.Len() != 0 {
		u := S.Remove(S.Front()).(int)
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == NIL {
				color[v] = c
				S.PushFront(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = NIL
	}
	for i := 0; i < n; i++ {
		if color[i] == NIL {
			id++
			DFS(i, id)
		}
	}
}

func main() {
	var s, t, m, q int
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < m; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if i == 0 {
			G[s] = make([]int, 0)
			G[t] = make([]int, 0)
		}
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}

	assignColor()
	scanner.Scan()
	q, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < q; i++ {
		scanner.Scan()
		s, _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		t, _ = strconv.Atoi(scanner.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

C++: #include <bits/stdc++.h>
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;
    }
  }
};

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <sstream>
#include <string>
#include <utility>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>

using namespace std;

class Friend
{
public:
  vector<int> v;
  int solve();
  bool isFriend( vector<vector<int>>& adj, vector<int>& visited, int u, int v );
};

bool Friend::isFriend( vector<vector<int>>& adj, vector<int>& visited, int u, int v )
{
  if ( u == v ) return true;

  if ( visited[u] ) return false;

  visited[u] = 1;

  int n = adj[u].size();
  
  for ( auto i = 0; i < n; ++i )
    {
      if ( isFriend( adj, visited, adj[u][i], v ) ) return true;
    }

  return false;
}

int Friend::solve()
{
  int n, m;
  cin >> n >> m;
  
  vector<vector<int>> adj( n );

  for ( auto i = 0; i < m; ++i )
    {
      int u, v;
      cin >> u >> v;
      adj[u].push_back( v );
      adj[v].push_back( u );
    }

  int q;
  cin >> q;

  for ( auto i = 0; i < q; ++i )
    {
      int u, v;
      cin >> u >> v;
      vector<int> visited( n, 0 );
      
      if ( isFriend( adj, visited, u, v ) )
        cout << "yes" << endl;
      else
        cout << "no" << endl;
    }
  
  return 0;
}

int main()
{
  Friend f;

  f.solve();

  return 0;
}

C++: #include<iostream>
using namespace std;
#include<stdio.h>

int *P;

int root(int a)
{
    if(P[a] == a) return a;
    P[a] = root(P[a]);
    return P[a]; 
}

void join(int a, int b){ int x = root(a), y = root(b); P[x] = y; }

bool same(int a, int b){ return (root(a) == root(b) ? true : false); }

int main()
{
    int i, n, m, a, b, q;
    scanf("%d %d", &n, &m);
    P = new int [n];
    for(i = 0; i < n; i++) P[i] = i;

    for(i = 0; i < m; i++){ scanf("%d %d", &a, &b); join(a, b); }

    scanf("%d", &q);
    for(i = 0; i < q; i++){
        scanf("%d %d", &a, &b);
        if(same(a, b)){ printf("yes\n"); }else{ printf("no\n"); }
    }
    delete [] P;
    return 0; 
}

C++: #include <bits/stdc++.h>
#define MAX 1000001
//#define EZPZ_LEMON_SQUEEZY
using namespace std;
vector<int> M[MAX];
int n, e, q;
bool vis[MAX];
int color[MAX];
void dfs(int index, int cor)
{
	vis[index] = 1;
	color[index] = cor;
	for (int i = 0; i < M[index].size(); i++) {
		if (!vis[M[index][i]]) {
			dfs(M[index][i], cor);
		}
	}
}

int main()
{
#ifdef EZPZ_LEMON_SQUEEZY
	freopen("D:\\out.txt", "w", stdout);
#endif
	ios::sync_with_stdio(0);
	int s, t;
	cin >> n >> e;
	for (int i = 0; i < e; i++) {
		cin >> s >> t;
		M[s].push_back(t);
		M[t].push_back(s);
	}
	memset(vis, 0, sizeof(vis));
	memset(color, 0, sizeof(color));
	int c = 1;
	for (int i = 0; i < n; i++) {
		if (!vis[i]) {
			dfs(i, c);
			c++;
		}
	}

	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> s >> t;
		if (color[s] == color[t] && color[s] != 0) cout << "yes" << endl;
		else cout << "no" << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
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++: // Ref : https://book.mynavi.jp/ec/products/detail/id=35408

#include <iostream>
#include <vector>
#include <stack>

using namespace std;
static const int NMAX = 100000;
static const int NIL = -1;

int n, group[NMAX];
vector<int> G[NMAX];

void depthFirstSearch(int ix, int id) {
	int y;
	stack<int> S;
	S.push(ix);
	while (!S.empty()) {
		ix = S.top(); S.pop();
		for (unsigned int j = 0; j < G[ix].size(); j++) {
			y = G[ix][j];
			if (group[y] == NIL) {
				group[y] = id;
				S.push(y);
			}
		}
	}
}

void classifyID() {
	int id = 0;
	for (int i = 0; i < n; i++) {
		if (group[i] == NIL) {
			depthFirstSearch(i, id);
			id++;
		}
	}
}

int main(void) {
	int m, x, y;

	cin >> n >> m;
	for (int i = 0; i < n; i++) group[i] = NIL; // initialize group list
	for (int i = 0; i < m; i++) { // create graph in linked list representation
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);
	}

	classifyID();

	cin >> n;

	for (int i = 0; i < n; i++) {
		cin >> x >> y;
		if ((group[x] != NIL) && (group[y] != NIL) && (group[x] == group[y]))
			cout << "yes";
		else
			cout << "no";
		cout << 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;
	if(d[a]!=d[b]){
		int k=d[b];
		for(int h=0;h<n;h++){
			if(d[h]==k){
				d[h]=d[a];
			}
		}
		
		
	}
}
cin>>q;
for(int y=0;y<q;y++){
	int a,b;
	cin>>a>>b;
	if(d[a]==d[b]){
		cout<<"yes"<<endl;
	}
	else{
		cout<<"no"<<endl;
	}
}
	return 0;
}