Go: package main

import (
	"fmt"
)

var n, m int
var g [][]int
var color []int

func main() {
	fmt.Scan(&n, &m)
	g = make([][]int, n) // SNSの関係を表すグラフ
	for i := 0; i < m; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		g[a] = append(g[a], b) // 無向重みなしグラフ
		g[b] = append(g[b], a)
	}
	color = make([]int, n) // 各ノードにつける色
	for i := 0; i < len(color); i++ {
		color[i] = -1 // 初期化しておく
	}
	type query struct {
		s, t int
	}
	var Q int
	fmt.Scan(&Q)
	qs := make([]query, Q)
	for i := 0; i < Q; i++ {
		fmt.Scan(&qs[i].s, &qs[i].t)
	}
	// fmt.Println("n", n, "m", m, "g", g, "Q", Q, "qs", qs)
	// 全ての頂点からdfsする
	id := 1
	for u := 0; u < n; u++ {
		if color[u] == -1 {
			dfs(u, id)
			id++
		}
	}
	// fmt.Println("color", color)
	for i := 0; i < Q; i++ {
		if color[qs[i].s] == color[qs[i].t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

func dfs(v, c int) {
	// fmt.Printf("dfs(v %d, c %d) en:\n", v, c)
	color[v] = c
	for _, vv := range g[v] {
		// fmt.Printf("  v %d, g[v] %v, vv %d\n", v, g[v], vv)
		if color[vv] == -1 {
			dfs(vv, c)
		}
	}
}

C++: #include <cassert>
#include <cstdio>
#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::scanf("%d %d", &n, &m);
    TGraph Graph(n);
    for(int i = 0; i < m; ++i){
        int s, t;
        std::scanf("%d %d", &s, &t);
        Graph.MakeFriend(s, t);
    }
    int q;
    std::scanf("%d", &q)
    Graph.SetCommunity();
    for(int i = 0; i < q; ++i){
        int s, t;
        std::scanf("%d %d", &s, &t);
        std::printf("%s\n", (Graph.IsSameCommunity(s, t) ? "yes" : "no"));
    }
    return 0;
    
}

C++: #include<stdio.h>
#include<vector>
using namespace std;

vector<vector<int> >g;
int n,m,q,s,t;

vector<bool>visited;
vector<int>color;
void dfs(int s){
    for(int i=0;i<g[s].size();i++){
        if(color[g[s][i]]==-1){
            color[g[s][i]]=color[s];
            dfs(g[s][i]);
        }
    }
}
int main() {
    scanf("%d %d",&n,&m);
    g.resize(n);
    color.resize(n,-1);
    for(int i=0;i<m;i++){
        scanf("%d %d",&s,&t);
        g[s].push_back(t);
    }
    for(int i=0;i<n;i++){
        if(color[i]==-1){
            color[i]=i;
            dfs(i);
        }
    }
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&s,&t);
        printf((color[s]==color[t]?"yes\n":"no\n"));
    }
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
int main()
{
    int n, m, q;
    scanf("%d %d", &n, &m);
    vector<vector<int> > a(n, vector<int>(0));
    for (int i = 0, j, k; i < m; i++)
    {
        scanf("%d %d", &j, &k);
        a[j].push_back(k);
        a[k].push_back(j);
    }
    vector<int> vis(n, -1);
    for (int i = 0; i < n; i++)
    {
        
        if (vis[i] == -1)
        {
            stack<int> b;
            b.push(i);
            vis[i] = i;
            for (int t; !b.empty(); )
            {
                t = b.top();
                b.pop();
                for (int j = 0; j < a[t].size(); j++)
                {
                    
                    if (vis[a[t][j]] == -1)
                    {
                        vis[a[t][j]] = i;
                        b.push(a[t][j]);
                    }
                }
            }
        }
    }
    
    scanf("%d", &q);
    for (int i = 0, j, k; i < q; i++)
    {
        scanf("%d %d", &j, &k);
        printf(vis[j] == vis[k] ? "yes\n" : "no\n");
    }
}