Go: package main

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

// Node 頂点
type Node struct {
	gid  int   // グループID
	edge []int // 隣接辺
}

// NoID グループIDなし
const NoID = -1

// NewNode 頂点の生成
func NewNode() *Node {
	return &Node{gid: NoID, edge: []int{}}
}

func parseNode(nodes []*Node, gid int, queue []int) {
	if len(queue) == 0 {
		return
	}
	var nextQ []int
	for _, id := range queue {
		node := nodes[id]
		if node.gid == NoID {
			node.gid = gid
			for _, edge := range node.edge {
				nextQ = append(nextQ, edge)
			}
		}
	}
	parseNode(nodes, gid, nextQ)
	return
}
func digNode(nodes []*Node, id, gid int) {
	node := nodes[id]
	if node.gid != NoID {
		// 探索済み
		return
	}
	node.gid = gid
	for _, edge := range node.edge {
		digNode(nodes, edge, gid)
	}
	return
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n, m := eGetInt2(scanner)

	nodes := make([]*Node, n)
	for i := 0; i < n; i++ {
		nodes[i] = NewNode()
	}

	for i := 0; i < m; i++ {
		a, b := eGetInt2(scanner)
		nodes[a].edge = append(nodes[a].edge, b)
		nodes[b].edge = append(nodes[b].edge, a)
	}

	for i := 0; i < n; i++ {
		// 幅優先探索
		// gid := i
		// queue := []int{i}
		// parseNode(nodes, gid, queue)

		// 深さ優先探索
		digNode(nodes, i, i)
	}

	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		a, b := eGetInt2(scanner)
		if nodes[a].gid == nodes[b].gid {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}
func eGetInt2(scanner *bufio.Scanner) (int, int) {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0], t[1]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

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