Go: package main

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

const MAX = 100000

type Queue struct {
	head      int
	tail      int
	container [MAX]int
}

func (q *Queue) push(n int) {
	q.container[q.tail] = n
	q.tail = (q.tail + 1) % MAX
}

func (q *Queue) pop() int {
	res := q.container[q.head]
	q.head = (q.head + 1) % MAX
	return res
}

func (q *Queue) isEmpty() bool {
	return q.head == q.tail
}

var sc = bufio.NewScanner(os.Stdin)
var route [][]int
var SEEN [MAX]bool
var GROUP [MAX]int

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func bfs(start, group int) {
	que := Queue{}
	que.push(start)

	for !que.isEmpty() {
		id := que.pop()

		if SEEN[id] {
			continue
		}
		SEEN[id] = true
		GROUP[id] = group

		for _, next := range route[id] {
			que.push(next)
		}
	}
}

func main() {
	n, m := scanInt(), scanInt()
	route = make([][]int, n)
	for i := 0; i < m; i++ {
		s, t := scanInt(), scanInt()
		route[s] = append(route[s], t)
		route[t] = append(route[t], s)
	}

	for i := 0; i < n; i++ {
		if !SEEN[i] {
			bfs(i, i+1)
		}
	}

	w := bufio.NewWriter(os.Stdout)
	q := scanInt()
	for i := 0; i < q; i++ {
		s, t := scanInt(), scanInt()
		if GROUP[s] == GROUP[t] {
			fmt.Fprintln(w, "yes")
		} else {
			fmt.Fprintln(w, "no")
		}
	}
	w.Flush()
}

C++: #include <stdio.h>
#include <stack>
#include <iostream>
using namespace std;
int G[100000][100000];
int vis[100000];
int n;

bool dfs(int s, int t)
{
    if (G[s][t] == 1 || G[t][s] == 1) return true;
    vis[s] = 1;
    for (int i = 0; i < n; i++)
    {
        if (G[s][i] == 1 && vis[i] == 0)
            {
                bool a = (dfs(i,t)); 
                if(a) 
                {
                    vis[s] = 0;
                    return a;
                }
            }

    } 
    vis[s] = 0;
    return false;
}

int main()
{
    freopen("in3.txt","r",stdin);
    int m;
    scanf("%d %d",&n, &m);
    for (int i = 0; i < n; i++)
    {
        vis[i] = 0; 
    } 
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            G[i][j] = 0; 
        }  
    } 
    for (int i = 0; i < m; i++)
    {
        int s, t;
        scanf("%d %d",&s,&t); 
        G[s][t] = 1;
        G[t][s] = 1;
    } 
    int q,s,t;
    scanf("%d",&q);
    for (int i = 0; i < q; i++)
    {
        scanf("%d %d",&s,&t);
        if(dfs(s,t)||dfs(t,s)) printf("yes\n");
        else printf("no\n");
    } 
    return 0;
}

C++: #include <iostream>
#include <vector>
#define N_MAX 100000
using namespace std;

vector<int> sns[N_MAX];
int* group;
int g = 0;

void dfs(int s) {
  if (group[s] != 0) return;
  group[s] = g;
  
  int len = sns[s].size();
  for (int i = 0; i < len; i++) {
    dfs(sns[s][i]);
  }
}

int main() {
  int n,m,q,s,t;
  cin >> n >> m;

  for (int i = 0; i < m; i++) {
    cin >> s >> t;

    sns[s].push_back(t);
    sns[t].push_back(s);
  }

  // dfs
  group = new int[n];
  for (int i = 0; i < n; i++) {
    if (group[i] == 0) {
      g++;
      dfs(i);
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (group[s] == group[t]) {
      cout << "yes" << endl;
    } else {
      cout << "no" << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cassert>
#include <queue>

const int M = 10000000;

void connected_components(
  std::vector<std::vector<int> >& adj_list,
  std::vector<int>& component_numbers,
  int root, int current_component){
  assert(component_numbers[root] == M);

  std::queue<int> target_q;
  target_q.push(root);
  component_numbers[root] = current_component;
  // dist_q.push(0;)
  while(!target_q.empty()){
    int vtx = target_q.front(); target_q.pop();
    for(int const&  u : adj_list[vtx]){
      if(component_numbers[u] == M){
        target_q.push(u);
        component_numbers[u] = current_component;
      }
    }
  }
}

int main(int argc, char const *argv[]) {
  int n, m, q;
  std::cin >> n;
  std::cin >> m;

  std::vector<std::vector<int> > adj_list(n);

  for (size_t i = 0; i < m; i++) {
    int a, b;
    std::cin >> a;
    std::cin >> b;
    adj_list[a].push_back(b);
  }

  std::vector<int> component(n, M);

  for (size_t i = 0; i < n; i++) {
    if(component[i] == M){
      connected_components(adj_list, component, i, i);
    }
  }

  std::cin >> q;
  for(size_t i =0; i < q; i++){
    int a, b;
    std::cin >> a;
    std::cin >> b;
    bool in_same_component = (component[a] == component[b]);
    std::cout << (in_same_component ? "yes" : "no") << std::endl;
  }


  return 0;
}

C++: #include <cstring>
#include <iostream>
#include <vector>

int main() {
    int n, m;
    std::cin >> n >> m;

    int link[n];
    std::vector<std::vector<int>> list;
    for (int i = 0; i < n; i++) {
        link[i] = -1;
    }

    int cnt = 0;
    int s, t;
    for (int i = 0; i < m; i++) {
        std::cin >> s >> t;
        if (link[s] == -1 && link[t] == -1) {
            link[s] = link[t] = cnt;
            cnt++;
            list.emplace_back(std::vector<int>({s, t}));
        } else if (link[s] == -1) {
            link[s] = link[t];
            list[link[t]].emplace_back(s);
        } else if (link[t] == -1) {
            link[t] = link[s];
            list[link[s]].emplace_back(t);
        } else if (link[s] < link[t]) {
            list[link[s]].insert(list[link[s]].end(), list[link[t]].begin(), list[link[t]].end());
            for (auto val : list[link[t]]) {
                link[val] = link[s];
            }
        } else if (link[t] < link[s]) {
            list[link[t]].insert(list[link[t]].end(), list[link[s]].begin(), list[link[s]].end());
            for (auto val : list[link[s]]) {
                link[val] = link[t];
            }
        }
    }

    int q;
    std::cin >> q;
    for (int i = 0; i < q; i++) {
        std::cin >> s >> t;
        if (link[s] == link[t] && link[s] != -1) {
            std::cout << "yes" << std::endl;
        } else {
            std::cout << "no" << std::endl;
        }
    }
}

C++: #include <iostream>
using namespace std;

int root(int x,int par[]){
    if (par[x] == x) return x;
    return par[x]=root(par[x],par);
}

int main(void){
    int n,m,q,s,t,i,rs,rt;
    cin>>n>>m;
    int par[100000];
    for(i=0;i<n;i++)par[i]=i;
    
    for(i=0;i<m;i++){
        cin>>s>>t;
        rs=root(s,par),rt=root(t,par);
        if(rs!=rt)par[rs]=rt;
    }

    for(i=0;i<n;i++){
        par[i]=root(i,par);
    }
    
    cin>>q;
    for(i=0;i<q;i++){
        cin>>s>>t;
        if(par[s]==par[t]){
            cout<<"yes"<<endl;
        }
        else{
            cout<<"no"<<endl;
        }
    }
    
    return 0;
}

C++: #include <iostream>
#include <stack>
#include <vector>
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);
			}
		}
	}
	return;
}

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++);
	}
	return;
}

int main() {
	int m;
	cin >> n >> m;
	int s, t;
	for (int i = 0; i < m; i++) {
		cin >> s >> t;
		G[s].push_back(t);
		G[t].push_back(s);
	}
	assignColor();
	int q;
	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;
}