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 <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;
}