Go: package main

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

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	nNodes := scanInt(sc)
	nConns := scanInt(sc)

	graph := make([][]int, nNodes)
	for i := 0; i < nConns; i++ {
		a, b := scanInt(sc), scanInt(sc)
		graph[a] = append(graph[a], b)
		graph[b] = append(graph[b], a)
	}

	groups := make([]int, nNodes)

	var setGroups func(int, int)
	setGroups = func(u int, groupId int) {
		if groups[u] > 0 {
			return
		}
		groups[u] = groupId

		for _, v := range graph[u] {
			setGroups(v, groupId)
		}
	}
	for i := 0; i < nNodes; i++ {
		setGroups(i, i+1)
	}

	nQuestions := scanInt(sc)

	w := bufio.NewWriter(os.Stdout)
	var ans string
	for i := 0; i < nQuestions; i++ {
		a, b := scanInt(sc), scanInt(sc)
		if groups[a] == groups[b] {
			ans = "yes"
		} else {
			ans = "no"
		}
		fmt.Fprintln(w, ans)
	}
	w.Flush()
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	v, _ := strconv.Atoi(sc.Text())
	return v
}

C++: #include<iostream>
#include<vector>
 
using namespace std;
 
int main(){
    int n, m, s, t, q, i, j;
    int id, color = 0;
    bool answer_yes = false;
 
    // enter
    cin >> n >> m;
    vector< vector<int> > Adj(n);
    vector<int> Stack;
    vector<int> Check(n, 0);
    vector<int>::iterator it, itend;
    for (i = 0; i < m; i++) {
        cin >> s >> t;
        Adj[s].push_back(t);
        Adj[t].push_back(s);
    }
    /*for (i = 0; i < n; i++) {
        cout << "\n" << i << " : " << flush;
        for (auto it2 = Adj[i].begin(); it2 != Adj[i].end(); it2++) {
            cout << *it2 << " " << flush;
        }
    }
    cout << endl;*/
 
    // calculate
    for (i = 0; i < n; i++) {
        if (Check[i] == 0) {
            color++;
            Stack.push_back(i);
            Check[i] = color;
            while (!Stack.empty()) {
                id = Stack.back();
                //cout << "\nid : " << id << " color : " << color << endl;
                itend = Adj[id].end();
                for (it = Adj[id].begin(); it != itend; it++) {
                    if (!Check[*it]) {
                        Stack.push_back(*it);
                        Check[*it] = color;
                        break;
                    }
                }
                if (it == itend) {
                    Stack.pop_back();
                }
            }
        }
    }
 
    //find
    cin >> q;
    for (i = 0; i < q; i++) {
        cin >> s >> t;      
        if (Check[s] == Check[t]) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
     
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int N = 1000000;
const int CRL = -1;
int v[N]={0};
void ketugou(int k,int l,int n){
	int x,mn;
	x = max(v[k],v[l]);
	mn = min(v[k],v[l]);
	for(int i=0;i<n;i++){
		if(v[i]==x)v[i]=mn;
	}
}

int main(){
	int n,m,q;
	memset(v, CRL, sizeof(v));
	cin >> n >> m;
	for(int i=0;i<m;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL && v[l]==CRL){
			v[k] = v[l] = min(k,l);
		}else if(v[k]!=CRL && v[l]!=CRL){
			ketugou(k,l,n);
		}else if(v[k]==CRL){
			v[k]=v[l];
		}else{
			v[l]=v[k];
		}
	}
	cin >> q;
	for(int i=0;i<q;i++){
		int k,l;
		cin >> k >> l;
		if(v[k]==CRL || v[l]==CRL) cout << "no" << endl;
		else if(v[k]==v[l])cout << "yes" << endl;
		else cout << "no" << endl;
	}
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#define MAX 100000

using namespace std;

int main(){
  int a[MAX]={};
  int n,m; cin>>n>>m;
  int s,t;
  int group=1;
  for(int i=0;i<m;i++){
    cin>>s>>t;
    if(a[s]==0 && a[t]==0){
      a[s]=a[t]=group;
      group++;
    }else if(a[s]==0){
      a[s]=a[t];
    }else if(a[t]==0){
      a[t]=a[s];
    }else{
      int tmp=a[t];
      a[t]=a[s];
      for(int j=0;j<n;j++){
        if(a[j]==tmp) a[j]=a[s];
      }
    }
  }

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

}

C++: #include <iostream>
#include "vector"
#include "stack"
using namespace std;
static const int mx=100000;
static const int nil=-1;

int n;
vector<int>g[mx];
int color[mx];

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 i=0;i<n;i++)
		if(color[i]==nil)dfs(i,id++);
}
int main(int argc, char *argv[]) {
	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 <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 <bits/stdc++.h>

using namespace std;


const int MAX_N = 1000000;
int par[MAX_N];

//syokika
void init(int n){
  for(int i = 0; i < n; i++) par[i] = i;
}

//kinonewomotomeru
int root(int x){
  if(par[x] == x){
    return x;
  }else{
    return par[x] = root(par[x]);
  }
}

//hanntei
bool same(int x, int y){
  return root(x) == root(y);
}

void unite(int x, int y){
  x = root(x);
  y = root(y);
  if(x == y) return;

  par[x] = y;
}

int main(void){

  int n, m;
  cin >> n >> m;

  vector<int> adj[n];

  //syokika
  init(n);

  //heigou
  for(int i = 0; i < m; i++){
    int s, t; cin >> s >> t;
    unite(s, t);
  }

  int q; cin >> q;

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


  return 0;

}