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 <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <list>
#include <bitset>

using namespace std;
typedef long long ll;

// #define DEBUG

int find(int a[], int x)
{
    while (a[x] != x)
    {
        a[x] = a[a[x]];
        x = a[x];
    }
    return x;
}

void unite(int a[], int x, int y)
{
    x = find(a, x);
    y = find(a, y);
    if (x != y)
    {
        a[x] = y;
    }
}

int main()
{
#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int n, m, q, x, y;
    cin >> n >> m;
    int p[n];
    for (int i = 0; i < n; i++)
        p[i] = i;

    for (int i = 0; i < m; i++)
    {
        cin >> x >> y;
        unite(p, x, y);
    }
    cin >> q;

    for (int i = 0; i < q; i++)
    {
        cin >> x >> y;
        cout << ((find(p, x) == find(p, y)) ? "yes" : "no") << endl;
    }
    return 0;
}

// getline(cin,s);
// a = strtok(s, ",");  ","で分解
// for (int i = 0; i < n; i++)

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) {
        S.push(v);
        color[v] = c;
      }
    }
  }
}

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

int main() {
  int m, u, v, q;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    G[u].push_back(v);
    G[v].push_back(u);
  }

  assignColor();

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> u >> v;
    if (color[u] == color[v]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << 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;
}

C++: #include <bits/stdc++.h>
using namespace std;

constexpr int MAX = 100010;

struct Edge{
    int v;
    Edge* next;
};

int N, M;
Edge* G[MAX];
int component[MAX];

void push( int u, int v ){
    Edge* e = new Edge;
    e->v = v;
    e->next = nullptr;

    Edge* i;
    for( i = G[u]; i->next != nullptr; i = i->next ){}
    i->next = e;
}

void dfs( int s, int k ){
    component[s] = k;
    for( Edge* e = G[s]->next; e != nullptr; e = e->next ){
        if( !component[e->v] ){
            dfs( e->v, k );
        }
    }
}

int main(){
    for( int i = 0; i < MAX; i++ ){
        Edge* e = new Edge;
        e->v = -1;
        e->next = nullptr;       
        G[i] = e;
    }
    
    cin >> N >> M;
    for( int i = 0; i < M; i++ ){
        int u, v; cin >> u >> v;
        push( u, v );
        push( v, u );
    }

    int k = 1;
    for( int i = 0; i < N; i++ ){
        if( !component[i] ){
            dfs( i, k );
            k++;
        }
    }

    int q; cin >> q;
    for( int i = 0; i < q; i++ ){
        int u, v; cin >> u >> v;
        if( component[u] == component[v] ){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }
}

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

void dfs(int node, std::vector<std::vector<int>>& v, std::vector<int>& colors, int color) {
    colors[node] = color;
    for (int i=0; i<v[node].size(); i++) {
        if (node != v[node][i] and colors[v[node][i]] == 0) {
            dfs(v[node][i], v, colors, color);
        }
    }
} 

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n, m, q, s, t, color=1;
    std::cin >> n >> m;
    std::vector<std::vector<int>> v(n);
    std::vector<int> colors(n);
    for (int i=0; i<m; i++) {
        std::cin >> s >> t;
        v[s].push_back(t);
        v[t].push_back(s);
    }
    for (int i=0; i<n; i++) {
        if (colors[i] == 0) {
            dfs(i, v, colors, color++);
        }
    }
    std::cin >> q;
    for (int i=0; i<q; i++) {
        std::cin >> s >> t;
        if (colors[s] == colors[t] and colors[s] != 0) {
            std::cout << "yes\n";
        } else {
            std::cout << "no\n";
        }
    }

    return 0;
}