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 <vector>

using namespace std;

bool dfs(int s, int t, vector<vector<int>> sns, bool* visit) {
  if (s == t) {
    return true;
  }
  if (visit[s]) {
    return false;
  }

  visit[s] = true;

  int len = sns[s].size();
  for (int i = 0; i < len; i++) {
    int next = sns[s][i];
    if (dfs(next, t, sns, visit)) {
      return true;
    }
  }
  return false;
}

int main() {
  int n,m,q,s,t;
  cin >> n >> m;

  vector<vector<int>> sns(n);
  
  for (int i = 0; i < m; i++) {
    cin >> s >> t;

    sns[s].push_back(t);
    sns[t].push_back(s);
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    bool* visit = new bool[n];
    if (dfs(s, t, sns, visit)) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
ll uni[100005];
ll root(ll u){
    if(uni[u]<0)return u;
    return uni[u]=root(uni[u]);
}

void connect(int a,int b){
    a=root(a);
    b=root(b);
    if(a==b)return;
    if(uni[a]<uni[b]){
        a^=b;
        b^=a;
        a^=b;
    }
    uni[a]+=uni[b];
    uni[b]=a;
    return;
}
ll size(ll u){
    return -uni[root(u)];
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    memset(uni,-1,sizeof(uni));
    int n,m,q,a,b;
    cin>>n>>m;
    for(int i=0;i<m;i++){
    	cin>>a>>b;
    	connect(a,b);
    }
    vector<string> v;
    cin>>q;
    for(int i=0;i<q;i++){
    	cin>>a>>b;
    	if(root(a)==root(b))v.pb("yes");
    	else v.pb("no");
    }
    for(int i=0;i<q;i++){
    	cout << v[i] <<endl;
    }
    return 0;
}

C++: // ALDS1_11_D.cpp
// Graph I - Connected Components

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

void bfs(vector<vector<int>> &sns, vector<int> &arrived, int x, int y)
{
    if (arrived[y] != 0) return;
    queue<int> mq;
    mq.push(x);
    while(!mq.empty()) {
        int curr = mq.front();
        mq.pop();
        arrived[curr] = 2;
        for (int i = 0; i < sns[curr].size(); i++) {
            int next = sns[curr][i];
            if (arrived[next] == 0) {
                mq.push(next);
                arrived[next] = 1;
            }
        }
    }
}

int main()
{
//    freopen("ALDS1_11_D_in22.txt", "r", stdin);
//    freopen("out22.txt", "w", stdout);
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>());
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x].push_back(y);
        sns[y].push_back(x);
    }

    cin >> q;
    while (q--) {
        vector<int> arrived(n);
        cin >> x >> y;
        bfs(sns, arrived, x, y);
        if (arrived[y]) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}

C++: // ALDS1_11_D.cpp
// Graph I - Connected Components

#include <iostream>
#include <vector>
using namespace std;

bool isFriend(vector<vector<int>> &sns, vector<int> &arrived, int x, int y)
{
    for (int i = 0; i < sns[x].size(); i++) {
        if (sns[x][i] == y) return true;
    }

    bool res = false;
    arrived[x] = 1;
    for (int i = 0; i < sns[x].size(); i++) {
        int next = sns[x][i];
        if (!arrived[next]) {
            bool tmp = isFriend(sns, arrived, next, y);
            if (tmp) {
                res = true;
                break;
            }
        }
    }
    return res;
}

int main()
{
//    freopen("ALDS1_11_D_in20.txt", "r", stdin);
//    freopen("out20.txt", "w", stdout);
    int m, n, q;
    cin >> n >> m;
    vector<vector<int> > sns(n, vector<int>());
    int x, y;
    for (int i = 0; i < m; i++) {
        cin >> x >> y;
        sns[x].push_back(y);
        sns[y].push_back(x);
    }

    cin >> q;
    while (q--) {
        vector<int> arrived(n);
        cin >> x >> y;
        bool res = isFriend(sns, arrived, x, y);
        if (res) cout << "yes" << endl;
        else cout << "no" << endl;
    }
    return 0;
}