Go: package main

import (
	"fmt"
	"os"
	"bufio"
	"strconv"
	"container/list"
	"bytes"
)

const MaxNode int = 1e5

var graph [][] int
var color []int
var N int

func bfs(u, c int) {
	l := list.New()
	l.PushBack(u)
	for ;l.Len()!=0; {
		u = l.Front().Value.(int)
		color[u] = c
		l.Remove(l.Front())
		for i:=0; i<len(graph[u]); i++ {
			if color[graph[u][i]] == 0 {
				l.PushBack(graph[u][i])
			}
		}
	}
}

func assignColor() {
	id := 1
	for u:=0; u<N; u++ {
		if color[u] == 0 {
			bfs(u, id)
			id ++
		}
	}
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num
}


func buildGraph(scanner *bufio.Scanner) {
	N = nextInt(scanner)
	color = make([]int, N)
	graph = make([][]int, N)
	m := nextInt(scanner)
	for i:=0; i<m; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		graph[n1] = append(graph[n1], n2)
		graph[n2] = append(graph[n2], n1)
	}
}

func answer(scanner *bufio.Scanner) {
	var outBuffer bytes.Buffer
	cnt := nextInt(scanner)
	for i:=0; i<cnt; i++ {
		n1 := nextInt(scanner)
		n2 := nextInt(scanner)
		if color[n1] == color[n2] {
			outBuffer.WriteString("yes\n")
		} else {
			outBuffer.WriteString("no\n")
		}
	}
	fmt.Printf("%s", outBuffer.String())
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	buildGraph(scanner)
	assignColor()
	answer(scanner)
}

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