Go: package main

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

var (
	n     int
	G     [100000][]int
	color [100000]*int
)

func dfs(r int, c int) {
	S := Stack{}
	S.Push(r)
	color[r] = &c
	for !S.IsEmpty() {
		u := S.Pop()
		for i := 0; i < len(G[u]); i++ {
			v := G[u][i]
			if color[v] == nil {
				color[v] = &c
				S.Push(v)
			}
		}
	}
}

func assignColor() {
	id := 1
	for i := 0; i < n; i++ {
		color[i] = nil
	}
	for u := 0; u < n; u++ {
		if color[u] == nil {
			id++
			dfs(u, id)
		}
	}
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	n, _ = strconv.Atoi(sc.Text())
	sc.Scan()
	r, _ := strconv.Atoi(sc.Text())

	for i := 0; i < r; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		G[s] = append(G[s], t)
		G[t] = append(G[t], s)
	}
	assignColor()
	sc.Scan()
	qNum, _ := strconv.Atoi(sc.Text())
	for i := 0; i < qNum; i++ {
		sc.Scan()
		s, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		if color[s] == color[t] {
			fmt.Println("yes")
		} else {
			fmt.Println("no")
		}
	}
}

// Stack is Stack
type Stack struct {
	stack []int
}

// Push new value
func (stack *Stack) Push(val int) {
	stack.stack = append(stack.stack, val)
}

// Pop new value
func (stack *Stack) Pop() int {
	val := stack.stack[len(stack.stack)-1]
	stack.stack = stack.stack[0 : len(stack.stack)-1]
	return val
}

// IsEmpty return true when stack is empty
func (stack *Stack) IsEmpty() bool {
	return len(stack.stack) == 0
}

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

C++: #include<iostream>
#include<vector>
#include<stack>
 
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;
 
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 ( int i = 0; i < G[u].size(); i++ ) {
         int v = G[u][i]
         if ( color[v] == NIL ) {
            color[v] = c;
            S.push(v);
         }
      }
   }
}

void assignColor() {
   int id = 1;
   for ( int i = 0; i < n; i++ ) color[i] = NIL;

   for ( int u = 0; u < n; u++ ) {
      if ( color[u] == NIL ) dfs(u, 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;
}