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 <cassert>
#include <algorithm>
#include <iostream>
#include <vector>
#include <list>

class TVertex
{
    int FIndex;
    int FCommuniyId;
    std::list<TVertex*> FList;

public:
    TVertex(int Index)
     : FIndex(Index), FCommuniyId(0)
    {}
    void AddPath(TVertex* Vertex)
    {
        FList.push_back(Vertex);
    }
    void SetCommunity(int CommunityId)
    {
        if(FCommuniyId) return;
        FCommuniyId = CommunityId;
        for(std::list<TVertex*>::iterator It = FList.begin(); It != FList.end(); ++It){
            TVertex* V = *It;
            V->SetCommunity(CommunityId);
        }
    }
    int CommunityId()const
    {
        return FCommuniyId;
    }
};
class TGraph
{
    std::vector<TVertex*> FTable;
    void Init(int n)
    {
        assert(n > 0);
        FTable.resize(n);
        for(int i = 0; i < n; ++i){
            FTable[i] = new TVertex(i);
        }
    }
public:
    TGraph(int n)
    {
        Init(n);
    }
    void MakeFriend(int s, int t)
    {
        assert(s < FTable.size());
        assert(t < FTable.size());
        FTable[s]->AddPath(FTable[t]);
        FTable[t]->AddPath(FTable[s]);
    }
    void SetCommunity()
    {
        for(std::size_t i = 0; i < FTable.size(); ++i){
            FTable[i]->SetCommunity(i+1);
        }
    }
    bool IsSameCommunity(int s, int t)const
    {
        assert(s < FTable.size());
        assert(t < FTable.size());
        return FTable[s]->CommunityId() == FTable[t]->CommunityId();
    }
};

int main()
{
    int n, m;
    std::cin >> n >> m;
    TGraph Graph(n);
    for(int i = 0; i < m; ++i){
        int s, t;
        std::cin >> s >> t;
        Graph.MakeFriend(s, t);
    }
    int q;
    std::cin >> q;
    Graph.SetCommunity();
    for(int i = 0; i < q; ++i){
        int s, t;
        std::cin >> s >> t;
        std::cout << (Graph.IsSameCommunity(s, t) ? "yes" : "no") << std::endl;
    }
    return 0;
    
}

C++: #include <bits/stdc++.h>

#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

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

    vvi g(n);

    rep(i, 0, m) {
        int s, t;
        cin >> s >> t;

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

    vi map(n, -1);
    vector<bool> seen(n, false);

    int t = 0;
    rep(i, 0, n) {
        if (seen[i]) { continue; }
        queue<int> st;
        st.push(i);
        seen[i] = true;
        map[i] = t;

        while (!st.empty()) {
            int v = st.front();
            st.pop();

            for (auto itr = g[v].begin(); itr != g[v].end(); ++itr) {
                if (!seen[*itr]) {
                    st.push(*itr);
                    seen[*itr] = true;
                    map[*itr] = t;
                }
            }
        }
        ++t;
    }

    cin >> q;
    rep(i, 0, q) {
        int s, t;
        cin >> s >> t;
        cout << (map[s] == map[t] ? "yes" : "no") << endl;
    }

    return 0;
}

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

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

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

bool TUser::IsAccesible(const TUser* Target)const
{
	return std::find(FAccessible.begin(), FAccessible.end(), 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)const;
};

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)const
{
	if(FUsers[UserA].IsAccesible(&FUsers[UserB]) || FUsers[UserB].IsAccesible(&FUsers[UserA])) 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;
}

C++: #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)

using namespace std;
using ll = long long;
using v1 = vector<int>;
using vl = vector<long long>;
using v2 = vector<vector<int>>;
using v3 = vector<vector<char>>;

int N;
vector<int> G[100000];
int color[100000];

void dfs(int s, int c){
	stack<int> S;
	S.push(s);
	color[s] = c;
	while(!S.empty()){
		int u = S.top();
		S.pop();
		rep(i, G[u].size()){
			int p = G[u][i];
			if(color[p] == 0){
				color[p] = c;
				S.push(p);
			}
		}
	}
}

void colorAssign(){
	rep(i, N){
		color[i] = 0;
	}
	int id = 1;
	rep(i, N){
		if(color[i] == 0){
			dfs(i, id);
			id++;
		}
	}

}

int main(){
	int m, s, t;
	cin >> N >> m;
	rep(i, m){
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	colorAssign();
	int q;
	cin >> q;
	rep(i, q){
		cin >> s >> t;
		if(color[s] == color[t]) cout << "yes" << endl;
		else cout << "no" << endl;
	}
}