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 <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>

using namespace std;
using ll = long long int;
#define rep(i,n) for(int i = 0; i < n; i++)
#define FOR(i, a, b)  for(int i = (a); i < (b) ; i++)

#define pb push_back
#define SORT(v,n) sort(v, v+n)
#define ALL(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << ": " << x << '\n'
#define elif else if
#define itn ll
#define int ll
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
///                →,↑,←,↓,↗,↖,↙,↘
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };

static const int MAX = 100000;
static const int NIL = -1;
int n, m;
vector<int> G[MAX];
int color[MAX];

void dfs(int r , int col) {
	stack<int> s;
	s.push(r);
	color[r] = col;
	while (!s.empty()) {
		int u = s.top();
		s.pop();
		int i;
		rep(i, G[u].size()) {
			int v = G[u][i];
			if (color[v] == NIL) {
				color[v] = col;
				s.push(v);
			}
		}
	}
}

void assignCol() {
	int col = 1;
	int i;
	rep(i, n) color[i] = NIL;
	rep(i, n) {
		if (color[i] == NIL)
			dfs(i,col++);
	}
}

signed main() {
	ios::sync_with_stdio(false);
	int q;
	cin >> n >> m;
	int i,s,t;
	rep(i, m) {
		cin >> s >> t;
		G[s].pb(t);
		G[t].pb(s);
	}
	assignCol();
	cin >> q;
	rep(i, q) {
		cin >> s >> t;
		if (color[s] == color[t])
			cout << "yes\n";
		else
			cout << "no\n";
	}
	return 0;
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var) \
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

struct Solver {
  explicit Solver(int n)
      : connected_(n, vector<bool>(n, false)), done_(n, false) {}
  void register_friend(int s, int t) {
    connected_[s][t] = connected_[t][s] = true;
  }
  bool can_reach(int s, int t) {
    if (s == t)
      return true;
    queue<int> q;
    q.push(s);
    while (!q.empty()) {
      auto v = q.front();
      q.pop();
      if (v == t)
        return true;
      if (done_[v])
        continue;
      done_[v] = true;
      auto& con = connected_[v];
      for (size_t i = 0; i < con.size(); ++i) {
        if (con[i]) {
          auto v2 = static_cast<int>(i);
          if (v2 == t)
            return true;
          if (!done_[v2]) {
            q.push(v2);
          }
        }
      }
    }
    return false;
  }

 private:
  vector<vector<bool>> connected_;
  vector<bool> done_;
};

int main() {
  int n, m;
  cin >> n >> m;
  Solver sol(n);
  for (int i = 0; i < m; ++i) {
    int s, t;
    cin >> s >> t;
    sol.register_friend(s, t);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int s, t;
    cin >> s >> t;
    auto ok = sol.can_reach(s, t);
    cout << (ok ? "yes" : "no") << endl;
  }
}

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

}