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>
#include<stack>
#include<algorithm>

bool dfs(std::vector<std::vector<int> > n, int from, int to) {
	std::stack<int> s;
	std::vector<bool> went(n.size(), false);

	s.push(from);
	while (s.size() != 0){
		int num = s.top(); s.pop();
		for (std::vector<int>::iterator it = n[num].begin(); it != n[num].end(); it++) {
			if (*it == to) return true;
			if (!went[*it]) {
				s.push(*it);
				went[*it] = true;
			}
		}
	}
	return false;
}

int main() {
	int people, relation;

	std::cin >> people >> relation;

	std::vector<std::vector<int> > n(people);

	for (int i = 0; i < relation; i++) {
		int from, to;
		std::cin >> from >> to;
		n[from].push_back(to);
		n[to].push_back(from);
	}

	int question;
	std::cin >> question;
	for (int i = 0; i < question; i++) {
		int from, to;
		std::cin >> from >> to;
		if (dfs(n, from, to)) {
			std::cout << "yes" << std::endl;
		} else {
			std::cout << "no" << std::endl;
		}
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<stack>

using namespace std;

#define MAX 10000

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]==-1)
			{
				color[v]=c;
				S.push(v);
			}
		}
	}
}

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

int main(int argc, char const *argv[])
{
	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++: #define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <clocale>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i)
#define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i)
#define VFOR(i, s, n) for(int i = (s); i < (n); ++i)
#define VFORS(i, s, n) for(int i = (s); i <= (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FORS(i, 0, n)
#define VREP(i, n) VFOR(i, 0, n)
#define VREPS(i, n) VFORS(i, 0, n)
#define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i)
#define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i)
#define RREP(i, n) RFOR(i, n, 0)
#define RREPS(i, n) RFORS(i, n, 0)
#define ALL(v) (v).begin(), (v).end()
#define SORT(v) sort(ALL(v))
#define RSORT(v) sort(ALL(v), greater<decltype(v[0])>())
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())

using ll = long long;
using Pi_i = pair<int, int>;
using VB = vector<bool>;
using VC = vector<char>;
using VD = vector<double>;
using VI = vector<int>;
using VLL = vector<ll>;
using VS = vector<string>;
using VSH = vector<short>;

const int MOD = 1000000007;
const int INF = 1000000000;
const int NIL = -1;

template<class T, class S>
bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S>
bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}


class UnionFind{
    vector<int> par;
    vector<int> rank;

public:
    UnionFind(){}
    UnionFind(int n): par(n), rank(n){
        REP(i, n){
            par[i] = i;
            rank[i] = 0;
        }
    }

    void resize(int n){
        int m = par.size();
        par.resize(n);
        rank.resize(n, 0);
        FOR(i, m, n) par[i] = i;
    }

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

    void unit(int x, int y){
        x = find(x);
        y = find(y);
        if(x == y) return;

        if(rank[x] < rank[y]){
            par[x] = y;
        }else{
            par[y] = x;
            if(rank[x] == rank[y]) ++rank[x];
        }
    }

    bool same(int x, int y){
        return find(x) == find(y);
    }
};
int n;
UnionFind UF;
vector<VI> edge;
VB visited;

void dfs(int u){
    visited[u] = true;
    for(int v: edge[u]){
        if(!visited[v]){
            UF.unit(u, v);
            dfs(v);
        }
    }
}

int main(){
    int m; cin >> n >> m;
    edge.resize(n);
    visited.resize(n, false);
    REP(i, m){
        int u, v; cin >> u >> v;
        edge[u].PB(v); edge[v].PB(u);
    }
    UF.resize(n);
    REP(i, n){
        if(!visited[i]) dfs(i);
    }
    int q; cin >> q;
    REP(i, q){
        int s, t; cin >> s >> t;
        cout << (UF.same(s, t) ? "yes" : "no") << "\n";
    }
    return 0;
}