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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = { 0, 1, 0, -1 }, dj[] = { 1, 0, -1, 0 };
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define MAX 100000
#define vmax 10000
#define sentinel 2000000000
#define INF (1<<30)
constexpr long long int inf = 1000000000000;
constexpr long long int rinf = -(INT64_MAX);
static const int NIL=-1;

//cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);

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(auto v:g[u]){
            if(color[v]==NIL){
                color[v]=c;
                s.push(v);
            }
        }
    }
}

void assigncolor(){
    int id=1;
    rep(i,n) color[i]=NIL;
    rep(i,n){
        if(color[i]==NIL){
            dfs(i,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++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#define null (-1)
using namespace std;
struct node{int fa,se;};
int n;
int color[100000];
vector<int> a[100000];
void dft(int s,int d){

	stack<int> sd;
	sd.push(s);
  color[s]=d;
	while(!sd.empty()){
		int qw=sd.top();sd.pop();
		for(int i=0;i<a[qw].size();i++){
			int wq=a[qw][i];
			if(color[wq]==null){
			color[wq]=d;
				sd.push(wq);
			}
		}
	}
}
void colors(){
int i,j=1;
	for(i=0;i<n;i++){
	color[i]=null;
}
	for(i=0;i<n;i++){
	if(color[i]==null) 
	dft(i,j++);
    
    }
}
main(){
	int i,j,m,q,fo,fs;
 node g[100000];
	scanf("%d %d",&n,&m);
	for(i=0;i<m;i++){
		cin>>fo>>fs;
		a[fo].push_back(fs);
		a[fs].push_back(fo);
	}
  cin>>q;
	for(i=0;i<q;i++){
		scanf("%d %d",&fo,&fs);	
     g[i].fa=fo;
      g[i].se=fs;
	}
	colors();
 for(i=0;i<q;i++){
	if(color[g[i].fa]==color[g[i].se])
		printf("yes\n");
		else
		printf("no\n");
 }
	return 0;
}

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

using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;

constexpr int nil = -1;

class Graph
{
public:

    Graph(int n)
        : e(n)
    {}

    void connect(int from, int to, bool isTwoWay = true)
    {
        e[from].push_back(to);
        if (isTwoWay)
            e[to].push_back(from);
    }

    vi getComponentGroup() const
    {
        vi group {vi(e.size(), nil)};
        int id = 0;

        for (int i = 0; i < e.size(); ++i)
        {
            if (group[i] != nil)
                continue;

             grouping(group, i, id++);
        }

        return group;
    }

private:
    vvi e;

    void grouping(vi& group, int vertex, int groupId) const
    {
        if (group[vertex] != nil)
            return;

        group[vertex] = groupId;

        for (auto v : e[vertex])
            grouping(group, v, groupId);
    }
};

void solve()
{
    using namespace std;

    int n, m;
    cin >> n >> m;

    Graph g {n};

    for (int i = 0, v1, v2; i < m; ++i)
    {
        cin >> v1 >> v2;
        g.connect(v1, v2);
    }

    auto group = g.getComponentGroup();

    int q;
    cin >> q;

    for (int i = 0, v1, v2; i < q; ++i)
    {
        cin >> v1 >> v2;
        cout << (group[v1] == group[v2] ? "yes" : "no") << endl;
    }
}

int main()
{
    solve();
    return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>

class TUser
{
	std::set<TUser*> FAccessible;
public:
	void AddFriend(TUser* Friend);
	bool IsAccesible(TUser* Target)const;
};

void TUser::AddFriend(TUser* Friend)
{
	if(IsAccesible(Friend)) return;
	FAccessible.insert(Friend);
	for(std::set<TUser*>::iterator It = FAccessible.begin(); It != FAccessible.end(); ++It){
		(*It)->AddFriend(Friend);
		Friend->AddFriend(*It);
	}
}

bool TUser::IsAccesible(TUser* Target)const
{
	return (FAccessible.find(Target) != FAccessible.end());
}

class TSns
{
	std::vector<TUser> FUsers;
public:
	TSns(int UserCount);
	
	void AddLink(int UserA, int UserB);
	std::string IsAccesible(int UserA, int UserB);
};

TSns::TSns(int UserCount)
: FUsers(UserCount)
{
}

void TSns::AddLink(int UserA, int UserB)
{
	FUsers[UserA].AddFriend(&FUsers[UserB]);
	FUsers[UserB].AddFriend(&FUsers[UserA]);
}

std::string TSns::IsAccesible(int UserA, int UserB)
{
	if(FUsers[UserA].IsAccesible(&FUsers[UserB])) return "yes";
	return "no";
}

int main()
{
	int UserCount;
	std::cin >> UserCount;
	TSns Sns(UserCount);
	
	int LinkCount;
	std::cin >> LinkCount;
	for(int i = 0; i < LinkCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		Sns.AddLink(UserA, UserB);
	}
	
	int QuestionCount;
	std::cin >> QuestionCount;
	for(int i = 0; i < QuestionCount; ++i){
		int UserA, UserB;
		std::cin >> UserA >> UserB;
		std::cout << Sns.IsAccesible(UserA, UserB) << std::endl;
	}
	return 0;
}