Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N, M := sc.nextInt(), sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < M; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	group := make([]int, N)
	for i := 0; i < N; i++ {
		group[i] = -1
	}
	var dfs func(cur int)
	dfs = func(cur int) {
		for i := 0; i < len(G[cur]); i++ {
			next := G[cur][i]
			if group[next] == -1 {
				group[next] = group[cur]
				dfs(next)
			}
		}
	}
	num := 0
	for i := 0; i < N; i++ {
		if group[i] == -1 {
			group[i] = num
			dfs(i)
			num++
		}
	}

	Q := sc.nextInt()
	for i := 0; i < Q; i++ {
		s, t := sc.nextInt(), sc.nextInt()
		if group[s] == group[t] {
			fmt.Fprintln(wtr, "yes")
		} else {
			fmt.Fprintln(wtr, "no")
		}
	}
	wtr.Flush()
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#define MAX_NODE 100000
#define MAX_EDGE 100000

using namespace std;

int n;//num of nodes
int e;//num of edges
int group[MAX_NODE+1];//0オリジン
int q;

bool neighbor_a[MAX_NODE+1];
vector<int> neighbor_v[MAX_NODE+1];//連接リスト

void addNeighbor(int num,int ori){//oriを含む連結グラフをつくる
	int ngb;
	if(neighbor_v[num].size() == 0){
		neighbor_a[neighbor_v[num][num]] = true;
		neighbor_v[ori].push_back(neighbor_v[num][num]);
		return;
	}
	for(int i = 0;i < neighbor_v[num].size();i++){//深さ優先探索
		if(neighbor_a[neighbor_v[num][i]] == false){
			neighbor_a[neighbor_v[num][i]] = true;
			neighbor_v[ori].push_back(neighbor_v[num][i]);
			ngb = neighbor_v[num][i];
			addNeighbor(ngb,ori);
		}
	}
}


int main(){
	int v_1,v_2;
	scanf("%d %d",&n,&e);

	for(int i = 0;i < e;i++){
		scanf("%d %d",&v_1,&v_2);
		neighbor_v[v_1].push_back(v_2);
		neighbor_v[v_2].push_back(v_1);
	}

	for(int i  = 0;i < n;i++) group[i] = -1;

	for(int i = 0;i < n;i++){
		if(group[i] != -1) continue;

		for(int j = 0;j < n;j++) neighbor_a[j] = false;//iの連接配列

		addNeighbor(i,i);

		for(int j = 0;j < neighbor_v[i].size();j++) group[neighbor_v[i][j]] = i;

	}
	//for(int i = 0;i < n;i++) cout <<group[i] <<" ";
	//for(int i = 0;i < neighbor_v[5].size();i++) cout <<neighbor_v[5][i] <<" ";

	scanf("%d",&q);

	for(int i = 0;i < q;i++){
		scanf("%d %d",&v_1,&v_2);
		if(group[v_1] == group[v_2]) cout <<"yes" <<endl;
		else cout <<"no" <<endl;
	}
	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]) || 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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <numeric>
#include <array>
#include <cmath>
#include <memory>
using namespace std;
//変数デバッグ
#define DEB(variable) cout << #variable << '=' << variable << endl

//for簡易表記(引数ミス防止)
#define FOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin); (LoopVariable) < (numberOFend); (LoopVariable)++)
#define DEFOR(LoopVariable,numberOFbegin,numberOFend) for (int LoopVariable = (numberOFbegin)-1; (LoopVariable) >= (numberOFend); (LoopVariable)--)
#define REP(LoopVariable,numberOFend) for(int LoopVariable = 0;(LoopVariable)<(numberOFend);LoopVariable++)

constexpr long long int linf = numeric_limits<long long int>::max()-5;
constexpr int inf= numeric_limits<int>::max()-5;
constexpr long long int mod = 1000000007;


//0~n-1というデータを扱うことが前提となっている
//unique_ptr<UFtree<int>> uf(new UFtree<int>(n));
template <typename T>
class UFtree{
public:
    UFtree(int data_num){
        v.resize(data_num);
        for (int i = 0; i < data_num; i++){
            v[i] = i;
        }
    }
    ~UFtree(){}

    T get_root(T a){
        T result;
        if(v[a]==v[v[a]]){
            return v[a];
        }else{
            T result;
            vector<T> path;
            T now_where=a;
            while(v[now_where]!=v[v[now_where]]){
                path.push_back(now_where);
                now_where=v[now_where];

            }
            for(T &x:path){
                v[x] = v[now_where];
            }
            return v[now_where];
        }
    }

    //aにbをつける
    void attach(T a,T b){
        v[get_root(b)] = v[get_root(a)];
    }

    
    
    void check(){for(T &x:v){
            cout << x<<" ";
        }
        cout << endl;
    }

  private:
    vector<T> v;


};



int main(){

    int n, m;
    cin >> n >> m;
    unique_ptr<UFtree<long long int>> uf(new UFtree<long long int>(n));

    long long int tmpa, tmpb;
    REP(i,m){
        scanf("%lld %lld", &tmpa, &tmpb);
        uf->attach(tmpa,tmpb);
    }

    int q;
    cin >> q;

    REP(i,q){
        scanf("%lld %lld", &tmpa, &tmpb);
        //cout << uf->get_root(tmpa) << " " << uf->get_root(tmpb) << endl;
        if(uf->get_root(tmpa)==uf->get_root(tmpb)){
            printf("yes\n");
        }else{
            printf("no\n");
        }
    }
    //uf->check();

    return 0;
}